1 // Copyright (c) 2015 Sergio Gonzalez. All rights reserved.
2 // License: https://github.com/serge-rgb/milton#license
3 
4 
5 #pragma once
6 
7 #include "utils.h"
8 #include "vector.h"
9 
10 struct Arena;
11 struct MiltonInput;
12 struct Milton;
13 struct PlatformState;
14 struct MiltonSettings;
15 
16 enum ColorPickerFlags
17 {
18     ColorPickerFlags_NOTHING = 0,
19 
20     ColorPickerFlags_WHEEL_ACTIVE    = (1 << 1),
21     ColorPickerFlags_TRIANGLE_ACTIVE = (1 << 2)
22 };
23 
24 struct PickerData
25 {
26     v2f a;  // Corresponds to value = 0      (black)
27     v2f b;  // Corresponds to saturation = 0 (white)
28     v2f c;  // Points to chosen hue.         (full color)
29 
30     v3f  hsv;
31 };
32 
33 struct ColorButton
34 {
35     i32 x;
36     i32 y;
37     i32 w;
38     i32 h;
39 
40     v4f rgba;
41 
42     ColorButton* next;
43 };
44 
45 struct ColorPicker
46 {
47     v2i     center;  // In screen pixel coordinates.
48     i32     bounds_radius_px;
49     Rect    bounds;
50     float   wheel_radius;
51     float   wheel_half_width;
52 
53     u32*    pixels;  // Blit this to render picker. Dimensions: picker_get_bounds(..)
54 
55     PickerData data;
56 
57     ColorButton* color_buttons;
58 
59     int flags;  // ColorPickerFlags
60 };
61 
62 enum ColorPickResult
63 {
64     ColorPickResult_NOTHING,
65     ColorPickResult_CHANGE_COLOR,
66 };
67 
68 struct GuiButton
69 {
70     Rect            rect;
71     Bitmap          bitmap;
72 };
73 
74 enum ExporterState
75 {
76     ExporterState_EMPTY,
77     ExporterState_GROWING_RECT,
78     ExporterState_SELECTED,
79 };
80 
81 struct Exporter
82 {
83     ExporterState state;
84     // Pivot: The raster point where we click to begin the rectangle
85     v2i pivot;
86     // Needle, the point that we drag.
87     v2i needle;
88 
89     int scale;
90 };
91 
92 // State machine for gui
93 enum MiltonGuiFlags
94 {
95     MiltonGuiFlags_NONE,
96 
97     MiltonGuiFlags_SHOWING_PREVIEW   = 1 << 0,
98 };
99 
100 struct MiltonGui
101 {
102     b32 menu_visible;
103     b32 visible;
104     b32 show_help_widget;
105 
106     b32 owns_user_input;
107     b32 did_hit_button;  // Avoid multiple clicks.
108 
109     int flags;  // MiltonGuiFlags
110 
111     i32 history;
112 
113     ColorPicker picker;
114 
115     Exporter exporter;
116 
117     v2i preview_pos;  // If rendering brush preview, this is where to do it.
118     v2i preview_pos_prev;  // Keep the previous position to clear the canvas.
119 
120     f32 scale;
121 
122     MiltonSettings* original_settings;
123 
124     char scratch_binding_key[Action_COUNT][2];
125 };
126 
127 //
128 // GUI API
129 //
130 // Call from the main loop before milton_update
131 
132 void milton_imgui_tick(MiltonInput* input, PlatformState* platform_state,  Milton* milton);
133 
134 
135 
136 //
137 void                gui_init(Arena* root_arena, MiltonGui* gui, f32 scale);
138 void                gui_toggle_menu_visibility(MiltonGui* gui);
139 void                gui_toggle_help(MiltonGui* gui);
140 v3f                 gui_get_picker_rgb(MiltonGui* gui);
141 
142 // Returns true if the GUI consumed input. False if the GUI wasn't affected
143 b32                 gui_consume_input(MiltonGui* gui, MiltonInput const* input);
144 void                gui_imgui_set_ungrabbed(MiltonGui* gui);
145 void                gui_picker_from_rgb(ColorPicker* picker, v3f rgb);
146 
147 void exporter_init(Exporter* exporter);
148 b32 exporter_input(Exporter* exporter, MiltonInput* input);  // True if exporter changed
149 
150 // Returns true if point is over a GUI element
151 b32 gui_point_hovers(MiltonGui* gui, v2i point);
152 
153 
154 // Color Picker API
155 void    picker_init(ColorPicker* picker);
156 b32     picker_hits_wheel(ColorPicker* picker, v2f point);
157 float   picker_wheel_get_angle(ColorPicker* picker, v2f point);
158 v3f     picker_hsv_from_point(ColorPicker* picker, v2f point);
159 Rect    picker_get_bounds(ColorPicker* picker);
160 Rect    get_bounds_for_picker_and_colors(ColorPicker* picker);
161 // When a selected color is used in a stroke, call this to update the color
162 // button list.
163 b32  gui_mark_color_used(MiltonGui* gui);
164 void gui_deactivate(MiltonGui* gui);
165 
166 // Eye Dropper
167 void eyedropper_input(MiltonGui* gui, u32* canvas_buffer, i32 w, i32 h, v2i point);
168 
169