Designer is working great now, and this fixes a bunch of things about the mouse.

This commit is contained in:
Zed A. Shaw 2024-12-12 10:14:43 -05:00
parent f05f652c26
commit ffc787df64
10 changed files with 4958 additions and 91 deletions

View file

@ -17,6 +17,7 @@
#include <vector>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <exception>
using namespace nlohmann;
using namespace fmt;
@ -39,19 +40,23 @@ struct FontGrid {
size_t width;
size_t height;
vector<vector<FontGridCell>> $grid;
string $font_list;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
vector<wstring> $wcharmap;
vector<string> $charmap;
FontGrid(size_t width, size_t height) :
FontGrid(string font_list, size_t width, size_t height) :
width(width), height(height),
$grid(height, vector<FontGridCell>(width, {0, "", L""}))
$grid(height, vector<FontGridCell>(width, {0, "", L""})),
$font_list(font_list)
{
configure_font();
}
void configure_font() {
std::ifstream in_file("./fontlist.json");
dbc::check(fs::exists($font_list), format("font listing {} does not exist", $font_list));
std::ifstream in_file($font_list);
json input = json::parse(in_file);
for(auto inchar : input) {
@ -120,7 +125,6 @@ struct WhatTheColor {
class GUI {
public:
DinkyECS::World& $world;
Panel $font_view;
Panel $status_ui;
Canvas $canvas;
@ -133,11 +137,10 @@ class GUI {
Component $fg_settings;
Component $bg_settings;
GUI(DinkyECS::World &world) :
$world(world),
GUI(string font_list) :
$font_view(GAME_MAP_POS, 0, GRID_SIZE.x, GRID_SIZE.y, true),
$status_ui(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
$font_grid(GRID_SIZE.x, GRID_SIZE.y),
$font_grid(font_list, GRID_SIZE.x, GRID_SIZE.y),
$fg_color{.h=20, .s=50, .v=20},
$bg_color{.h=100, .s=100, .v=100}
{
@ -157,12 +160,11 @@ class GUI {
}
void draw_font_grid() {
int flip_it = 0;
for(size_t y = 0; y < $font_grid.height; y++) {
for(size_t x = 0; x < $font_grid.width; x++, flip_it++) {
for(size_t x = 0; x < $font_grid.width; x++) {
$canvas.DrawText(x * 2, y * 4, $font_grid.as_string(x, y), [&](auto &pixel) {
pixel.foreground_color = Color::HSV($fg_color.h, $fg_color.s, $fg_color.v / (flip_it % 2 + 1));
pixel.background_color = Color::HSV($bg_color.h, $bg_color.s, $bg_color.v / (flip_it % 2 + 1));
pixel.foreground_color = Color::HSV($fg_color.h, $fg_color.s, $fg_color.v);
pixel.background_color = Color::HSV($bg_color.h, $bg_color.s, $bg_color.v);
});
}
}
@ -238,6 +240,7 @@ class GUI {
bool event_happened;
using KB = sf::Keyboard;
sf::Event event;
Point pos;
int font_size = $renderer.font_size();
int page_size = $font_grid.page_size();
@ -255,22 +258,29 @@ class GUI {
$start_char = std::min($font_grid.max_chars() - page_size, $start_char + page_size);
render_grid($start_char, false);
$renderer.clear_cache();
} else if(KB::isKeyPressed(KB::Tab)) {
$status_ui.key_press(Event::Tab);
} else if(KB::isKeyPressed(KB::Tab)) {
$status_ui.key_press(Event::Return);
} else if(KB::isKeyPressed(KB::Equal)) {
resize_fonts(font_size + 10);
} else if(KB::isKeyPressed(KB::Hyphen)) {
resize_fonts(font_size - 10);
event_happened = true;
}
} else if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
Point pos;
if($renderer.mouse_position($font_view, pos)) {
} else if($renderer.mouse_position($font_view, pos)) {
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
select_cell(pos);
event_happened = true;
} else if($renderer.mouse_position($status_ui, pos)) {
$status_ui.mouse_click(Mouse::Button::Left, pos);
} else if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
deselect_cell();
}
} else if($renderer.mouse_position($status_ui, pos)) {
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
$status_ui.mouse_click(Mouse::Button::Left, pos);
} else if(event.type == sf::Event::MouseMoved) {
$status_ui.mouse_release(Mouse::Button::Left, pos);
}
} else if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
deselect_cell();
}
}
@ -291,20 +301,26 @@ class GUI {
};
int main(int argc, char *argv[]) {
DinkyECS::World world;
try {
dbc::check(argc == 2, "USAGE: designer fontlist.json");
GUI gui(world);
string font_list{argv[1]};
gui.create_renderer();
GUI gui(font_list);
do {
gui.render_scene();
gui.create_renderer();
if(gui.handle_ui_events()) {
}
do {
gui.render_scene();
std::this_thread::sleep_for(10ms);
} while(gui.$renderer.is_open());
if(gui.handle_ui_events()) {
}
return 0;
std::this_thread::sleep_for(10ms);
} while(gui.$renderer.is_open());
return 0;
} catch(const dbc::Error &e) {
println("ERROR: {}", e.message);
}
}