-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing view and element drop support
- Loading branch information
Showing
19 changed files
with
388 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
cmake_minimum_required(VERSION 3.9.6...3.15.0) | ||
project(HelloUniverse LANGUAGES C CXX) | ||
|
||
if (NOT ELEMENTS_ROOT) | ||
message(FATAL_ERROR "ELEMENTS_ROOT is not set") | ||
endif() | ||
|
||
# Make sure ELEMENTS_ROOT is an absolute path to add to the CMake module path | ||
get_filename_component(ELEMENTS_ROOT "${ELEMENTS_ROOT}" ABSOLUTE) | ||
set (CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${ELEMENTS_ROOT}/cmake") | ||
|
||
# If we are building outside the project, you need to set ELEMENTS_ROOT: | ||
if (NOT ELEMENTS_BUILD_EXAMPLES) | ||
include(ElementsConfigCommon) | ||
set(ELEMENTS_BUILD_EXAMPLES OFF) | ||
add_subdirectory(${ELEMENTS_ROOT} elements) | ||
endif() | ||
|
||
set(ELEMENTS_APP_PROJECT "DropFile") | ||
set(ELEMENTS_APP_TITLE "Drop File") | ||
set(ELEMENTS_APP_COPYRIGHT "Copyright (c) 2023 Joel de Guzman") | ||
set(ELEMENTS_APP_ID "com.cycfi.drop-file") | ||
set(ELEMENTS_APP_VERSION "1.0") | ||
|
||
set(ELEMENTS_APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) | ||
set(ELEMENTS_APP_RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/resources/space.jpg) | ||
|
||
# For your custom application icon on macOS or Windows see cmake/AppIcon.cmake module | ||
include(AppIcon) | ||
include(ElementsConfigApp) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/*============================================================================= | ||
Copyright (c) 2016-2020 Joel de Guzman | ||
Distributed under the MIT License (https://opensource.org/licenses/MIT) | ||
=============================================================================*/ | ||
#include <elements.hpp> | ||
|
||
using namespace cycfi::elements; | ||
|
||
using cycfi::artist::rgba; | ||
|
||
// Main window background color | ||
auto constexpr bkd_color = rgba(35, 35, 37, 255); | ||
auto background = box(bkd_color); | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
app _app(argc, argv, "Drop File", "com.cycfi.drop-file"); | ||
window _win(_app.name()); | ||
_win.on_close = [&_app]() { _app.stop(); }; | ||
|
||
view view_(_win); | ||
|
||
auto image_ = share(image{ "space.jpg" }); | ||
auto drop_box_ = share(drop_box(scroller(hold(image_)))); | ||
|
||
drop_box_->on_drop = [image_ = get(image_), &view_](drop_info const& info) | ||
{ | ||
if (info.paths.size() == 1) // We accept only one file | ||
{ | ||
auto image_path = info.paths[0]; | ||
if (auto p = image_.lock()) | ||
{ | ||
try | ||
{ | ||
auto img = image{image_path}; | ||
*p = img; | ||
view_.refresh(*p); | ||
} | ||
catch (std::runtime_error const&) | ||
{ | ||
// Invalid image | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
view_.content( | ||
align_center_middle(label("Drop a picture here").font_size(20)), | ||
margin({20, 20, 20, 20}, hold(drop_box_)), | ||
share(background) | ||
); | ||
|
||
_app.run(); | ||
return 0; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/*============================================================================= | ||
Copyright (c) 2016-2020 Joel de Guzman | ||
Distributed under the MIT License [ https://opensource.org/licenses/MIT ] | ||
=============================================================================*/ | ||
#if !defined(ELEMENTS_DROP_OCOBER_11_2023) | ||
#define ELEMENTS_DROP_OCOBER_11_2023 | ||
|
||
#include <elements/element/proxy.hpp> | ||
#include <functional> | ||
|
||
namespace cycfi { namespace elements | ||
{ | ||
template <typename Subject> | ||
class drop_box : public proxy<Subject> | ||
{ | ||
public: | ||
|
||
using base_type = proxy<Subject>; | ||
using on_drop_function = std::function<bool(drop_info const& info)>; | ||
|
||
drop_box(Subject subject); | ||
void draw(context const& ctx) override; | ||
bool wants_control() const override; | ||
|
||
void track_drop(context const& ctx, drop_info const& info, cursor_tracking status) override; | ||
bool drop(context const& ctx, drop_info const& info) override; | ||
|
||
on_drop_function on_drop = [](drop_info const&){ return false; }; | ||
|
||
private: | ||
|
||
bool _is_tracking = false; | ||
}; | ||
|
||
template <typename Subject> | ||
drop_box<Subject>::drop_box(Subject subject) | ||
: proxy<Subject>{std::move(subject)} | ||
{} | ||
|
||
template <typename Subject> | ||
inline void drop_box<Subject>::draw(context const& ctx) | ||
{ | ||
proxy<Subject>::draw(ctx); | ||
if (_is_tracking) | ||
{ | ||
auto& cnv = ctx.canvas; | ||
auto& bounds = ctx.bounds; | ||
cnv.stroke_style(get_theme().indicator_hilite_color.opacity(0.5)); | ||
cnv.line_width(2.0); | ||
cnv.add_rect(bounds); | ||
cnv.stroke(); | ||
} | ||
} | ||
|
||
template <typename Subject> | ||
inline bool drop_box<Subject>::wants_control() const | ||
{ | ||
return true; | ||
} | ||
|
||
template <typename Subject> | ||
inline void drop_box<Subject>::track_drop(context const& ctx, drop_info const& /*info*/, cursor_tracking status) | ||
{ | ||
auto new_is_tracking = status != cursor_tracking::leaving; | ||
if (new_is_tracking != _is_tracking) | ||
{ | ||
_is_tracking = new_is_tracking; | ||
ctx.view.refresh(ctx.bounds); | ||
} | ||
} | ||
|
||
template <typename Subject> | ||
inline bool drop_box<Subject>::drop(context const& ctx, drop_info const& info) | ||
{ | ||
bool r = on_drop(info); | ||
_is_tracking = false; | ||
ctx.view.refresh(ctx.bounds); | ||
return r; | ||
} | ||
}} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.