|
| 1 | +// LAF Library |
| 2 | +// Copyright (c) 2024 Igara Studio S.A. |
| 3 | +// |
| 4 | +// This file is released under the terms of the MIT license. |
| 5 | +// Read LICENSE.txt for more information. |
| 6 | + |
| 7 | + |
| 8 | +#include "base/paths.h" |
| 9 | +#include "gfx/hsv.h" |
| 10 | +#include "gfx/point.h" |
| 11 | +#include "gfx/rect.h" |
| 12 | +#include "gfx/rgb.h" |
| 13 | +#include "os/dnd.h" |
| 14 | +#include "os/draw_text.h" |
| 15 | +#include "os/os.h" |
| 16 | +#include "os/paint.h" |
| 17 | +#include "os/surface.h" |
| 18 | + |
| 19 | +#include <algorithm> |
| 20 | +#include <cstdarg> |
| 21 | +#include <cstdio> |
| 22 | +#include <map> |
| 23 | +#include <memory> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +using Boxes = std::vector<gfx::Rect>; |
| 27 | + |
| 28 | +struct WindowData { |
| 29 | + bool dragEnter; |
| 30 | + bool dragLeave; |
| 31 | + int drag; |
| 32 | + base::paths paths; |
| 33 | + os::SurfaceRef image; |
| 34 | + gfx::Point dragPosition; |
| 35 | + gfx::Rect dropZone; |
| 36 | +}; |
| 37 | + |
| 38 | +static WindowData windowData; |
| 39 | + |
| 40 | +static void redraw_window(os::Window* window); |
| 41 | + |
| 42 | +class DragTarget : public os::DragTarget { |
| 43 | +public: |
| 44 | + void dragEnter(os::DragEvent& ev) override { |
| 45 | + if (!windowData.dropZone.contains(ev.position()) || !ev.sourceSupports(os::DropOperation::Copy)) |
| 46 | + ev.dropResult(os::DropOperation::None); |
| 47 | + else if (ev.sourceSupports(os::DropOperation::Copy)) |
| 48 | + ev.dropResult(os::DropOperation::Copy); |
| 49 | + |
| 50 | + windowData.dragEnter = true; |
| 51 | + windowData.dragLeave = false; |
| 52 | + windowData.drag = 0; |
| 53 | + windowData.dragPosition = ev.position(); |
| 54 | + redraw_window(ev.target()); |
| 55 | + ev.target()->invalidate(); |
| 56 | + } |
| 57 | + void dragLeave(os::DragEvent& ev) override { |
| 58 | + windowData.dragEnter = false; |
| 59 | + windowData.dragLeave = true; |
| 60 | + windowData.dragPosition = ev.position(); |
| 61 | + redraw_window(ev.target()); |
| 62 | + ev.target()->invalidate(); |
| 63 | + } |
| 64 | + void drag(os::DragEvent& ev) override { |
| 65 | + ++windowData.drag; |
| 66 | + windowData.dragPosition = ev.position(); |
| 67 | + redraw_window(ev.target()); |
| 68 | + ev.target()->invalidate(); |
| 69 | + } |
| 70 | + void drop(os::DragEvent& ev) override { |
| 71 | + windowData.dragEnter = false; |
| 72 | + windowData.dragLeave = false; |
| 73 | + windowData.dragPosition = {0, 0}; |
| 74 | + ev.acceptDrop(windowData.dropZone.contains(ev.position())); |
| 75 | + |
| 76 | + if (ev.acceptDrop()) { |
| 77 | + if (ev.dataProvider()->contains(os::DragDataItemType::Paths)) |
| 78 | + windowData.paths = ev.dataProvider()->getPaths(); |
| 79 | + if (ev.dataProvider()->contains(os::DragDataItemType::Image)) |
| 80 | + windowData.image = ev.dataProvider()->getImage(); |
| 81 | + } |
| 82 | + |
| 83 | + redraw_window(ev.target()); |
| 84 | + ev.target()->invalidate(); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +static void redraw_window(os::Window* window) |
| 89 | +{ |
| 90 | + os::Surface* s = window->surface(); |
| 91 | + os::Paint paint; |
| 92 | + paint.color(gfx::rgba(0, 0, 0)); |
| 93 | + s->drawRect(window->bounds(), paint); |
| 94 | + |
| 95 | + paint.color(gfx::rgba(255, 255, 255)); |
| 96 | + |
| 97 | + char buf[256]; |
| 98 | + int y = 12; |
| 99 | + std::snprintf(buf, sizeof(buf), "Drag Position = [%d, %d]", windowData.dragPosition.x, windowData.dragPosition.y); |
| 100 | + os::draw_text(s, nullptr, buf, gfx::Point(0, y), &paint); |
| 101 | + y += 12; |
| 102 | + std::snprintf(buf, sizeof(buf), "Drag Enter = %s", windowData.dragEnter ? "true" : "false"); |
| 103 | + os::draw_text(s, nullptr, buf, gfx::Point(0, y), &paint); |
| 104 | + y += 12; |
| 105 | + std::snprintf(buf, sizeof(buf), "Drag = %d", windowData.drag); |
| 106 | + os::draw_text(s, nullptr, buf, gfx::Point(0, y), &paint); |
| 107 | + y += 12; |
| 108 | + std::snprintf(buf, sizeof(buf), "Drag Leave = %s", windowData.dragLeave ? "true" : "false"); |
| 109 | + os::draw_text(s, nullptr, buf, gfx::Point(0, y), &paint); |
| 110 | + |
| 111 | + if (!windowData.paths.empty()) { |
| 112 | + y += 12; |
| 113 | + std::snprintf(buf, sizeof(buf), "Paths = %lu", windowData.paths.size()); |
| 114 | + os::draw_text(s, nullptr, buf, gfx::Point(0, y), &paint); |
| 115 | + for (const auto& path : windowData.paths) { |
| 116 | + y += 12; |
| 117 | + std::snprintf(buf, sizeof(buf), "%s", path.c_str()); |
| 118 | + os::draw_text(s, nullptr, buf, gfx::Point(12, y), &paint); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (windowData.image) { |
| 123 | + y += 12; |
| 124 | + s->drawSurface(windowData.image.get(), 0, y); |
| 125 | + } |
| 126 | + |
| 127 | + paint.style(os::Paint::Style::Stroke); |
| 128 | + s->drawRect(window->bounds(), paint); |
| 129 | + |
| 130 | + |
| 131 | + auto zoneColor = gfx::rgba(100, 255, 100); |
| 132 | + auto textColor = zoneColor; |
| 133 | + if (windowData.dropZone.contains(windowData.dragPosition)){ |
| 134 | + paint.style(os::Paint::Style::Fill); |
| 135 | + paint.color(zoneColor); |
| 136 | + s->drawRect(windowData.dropZone, paint); |
| 137 | + textColor = gfx::rgba(0, 0, 0); |
| 138 | + } |
| 139 | + |
| 140 | + paint.color(zoneColor); |
| 141 | + paint.style(os::Paint::Style::Stroke); |
| 142 | + s->drawRect(windowData.dropZone, paint); |
| 143 | + |
| 144 | + paint.color(textColor); |
| 145 | + paint.style(os::Paint::Style::Fill); |
| 146 | + os::draw_text(s, nullptr, "Drop here!", windowData.dropZone.center(), &paint, os::TextAlign::Center); |
| 147 | +} |
| 148 | + |
| 149 | +static os::WindowRef create_window(const std::string& title, |
| 150 | + const os::WindowSpec& spec, |
| 151 | + os::DragTarget& dragTarget) |
| 152 | +{ |
| 153 | + os::WindowRef newWindow = os::instance()->makeWindow(spec); |
| 154 | + newWindow->setCursor(os::NativeCursor::Arrow); |
| 155 | + newWindow->setTitle(title); |
| 156 | + newWindow->setVisible(true); |
| 157 | + newWindow->setDragTarget(&dragTarget); |
| 158 | + windowData.dropZone = {32, spec.frame().h - 64 - 40, 64, 64}; |
| 159 | + redraw_window(newWindow.get()); |
| 160 | + return newWindow; |
| 161 | +} |
| 162 | + |
| 163 | +int app_main(int argc, char* argv[]) |
| 164 | +{ |
| 165 | + auto system = os::make_system(); |
| 166 | + system->setAppMode(os::AppMode::GUI); |
| 167 | + system->handleWindowResize = redraw_window; |
| 168 | + |
| 169 | + auto screen = system->mainScreen(); |
| 170 | + os::WindowSpec spec; |
| 171 | + DragTarget dragTarget; |
| 172 | + spec.titled(true); |
| 173 | + spec.position(os::WindowSpec::Position::Frame); |
| 174 | + auto frame = screen->workarea()/2; |
| 175 | + spec.frame(frame); |
| 176 | + spec.screen(screen); |
| 177 | + os::WindowRef window = create_window("Drag & Drop example", spec, dragTarget); |
| 178 | + |
| 179 | + bool running = true; |
| 180 | + |
| 181 | + system->finishLaunching(); |
| 182 | + system->activateApp(); |
| 183 | + |
| 184 | + os::EventQueue* queue = system->eventQueue(); |
| 185 | + os::Event ev; |
| 186 | + while (running) { |
| 187 | + queue->getEvent(ev); |
| 188 | + |
| 189 | + switch (ev.type()) { |
| 190 | + |
| 191 | + case os::Event::CloseApp: |
| 192 | + case os::Event::CloseWindow: |
| 193 | + running = false; |
| 194 | + break; |
| 195 | + |
| 196 | + case os::Event::ResizeWindow: |
| 197 | + redraw_window(ev.window().get()); |
| 198 | + ev.window()->invalidate(); |
| 199 | + break; |
| 200 | + |
| 201 | + default: |
| 202 | + // Do nothing |
| 203 | + break; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return 0; |
| 208 | +} |
0 commit comments