Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Implement open polygon logic #7

Merged
merged 1 commit into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/app.v
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ fn create_widgets() []ui.Widget {
viewport_app := viewport.create_viewport_app()

return [
widgets.create_menubar_widget(viewport_app.open_work_sprite, viewport_app.set_polygon_file_path,
viewport_app.save_polygon),
widgets.create_menubar_widget(viewport_app.open_work_sprite, viewport_app.open_polygon,
viewport_app.set_polygon_file_path, viewport_app.save_polygon),
viewport.create_viewport_widget(viewport_app),
]
}
35 changes: 29 additions & 6 deletions src/viewport/viewport.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import artemkakun.trnsfrm2d
import os
import x.json2
import artemkakun.pcoll2d
import json

const viewport_z_index = 1

Expand Down Expand Up @@ -72,6 +73,25 @@ pub fn (mut app ViewportApp) open_work_sprite(path_to_sprite string) ! {
app.work_sprite = app.gg.create_image(path_to_sprite)!
}

// open_polygon opens a polygon shape from the provided path for editing in the viewport.
pub fn (mut app ViewportApp) open_polygon(path_to_polygon string) ! {
polygon_data := os.read_file(path_to_polygon)!
polygon := json.decode(pcoll2d.Polygon, polygon_data)!
position := get_work_sprite_position(app)!

mut global_polygon_points := []trnsfrm2d.Position{}

for point in polygon.points {
global_polygon_points << trnsfrm2d.Position{
x: point.x * work_sprite_scale + position.x
y: point.y * work_sprite_scale + position.y
}
}

app.polygon_points = global_polygon_points
app.set_polygon_file_path(path_to_polygon)
}

// set_polygon_file_path sets the path to the polygon file that will be used for editing in the viewport.
pub fn (mut app ViewportApp) set_polygon_file_path(path_to_polygon string) {
app.polygon_file_path = path_to_polygon
Expand All @@ -88,12 +108,7 @@ pub fn (app &ViewportApp) save_polygon() {
}

fn create_polygon_data_in_json_form(app ViewportApp) !string {
sprite_to_draw := app.work_sprite or {
return error("No work sprite, polygon points can't be calculated!")
}

position, _ := calculate_work_sprite_transforms(sprite_to_draw, app.bounds)

position := get_work_sprite_position(app)!
mut local_polygon_points := []trnsfrm2d.Position{}

for global_polygon_point in app.polygon_points {
Expand All @@ -106,3 +121,11 @@ fn create_polygon_data_in_json_form(app ViewportApp) !string {
polygon := pcoll2d.Polygon{local_polygon_points}
return json2.encode[pcoll2d.Polygon](polygon)
}

fn get_work_sprite_position(app ViewportApp) !trnsfrm2d.Position {
sprite_to_draw := app.work_sprite or { return error('No work sprite!') }

position, _ := calculate_work_sprite_transforms(sprite_to_draw, app.bounds)

return position
}
28 changes: 18 additions & 10 deletions src/widgets/menubar.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,37 @@ const (
)

// create_menubar_widget returns menubar widget.
pub fn create_menubar_widget(load_sprite_to_viewport_function fn (string) !, set_polygon_file_function fn (string), save_polygon fn ()) ui.Widget {
pub fn create_menubar_widget(load_sprite_to_viewport_function fn (string) !, open_polygon_file fn (string) !, set_polygon_file_function fn (string), save_polygon fn ()) ui.Widget {
return ui.menubar(
height: widgets.menubar_height
z_index: widgets.menubar_z_index
items: [
ui.menuitem(
text: widgets.menubar_file_button_text
submenu: create_file_menu(load_sprite_to_viewport_function, set_polygon_file_function,
save_polygon)
submenu: create_file_menu(load_sprite_to_viewport_function, open_polygon_file,
set_polygon_file_function, save_polygon)
),
]
)
}

fn create_file_menu(load_sprite_to_viewport_function fn (string) !, set_polygon_file_function fn (string), save_polygon fn ()) &ui.Menu {
fn create_file_menu(load_sprite_to_viewport_function fn (string) !, open_polygon_file fn (string) !, set_polygon_file_function fn (string), save_polygon fn ()) &ui.Menu {
open_sprite_file_function := fn [load_sprite_to_viewport_function] (_ &ui.MenuItem) {
sprite_path := open_sprite_file_dialog_with_kdialog()
sprite_path := open_file_dialog_with_kdialog('.png')

load_sprite_to_viewport_function(sprite_path) or {
eprintln('Failed to load sprite file from path ${sprite_path}: ${err}')
}
}

open_polygon_file_function := fn [open_polygon_file] (_ &ui.MenuItem) {
polygon_path := open_file_dialog_with_kdialog('.json')

open_polygon_file(polygon_path) or {
eprintln('Failed to load polygon file from path ${polygon_path}: ${err}')
}
}

create_polygon_file_function := fn [set_polygon_file_function] (_ &ui.MenuItem) {
polygon_path := open_polygon_file_save_dialog_with_kdialog() or {
eprintln('Error while selecting save path for new polygon file - ${err}')
Expand All @@ -48,7 +56,7 @@ fn create_file_menu(load_sprite_to_viewport_function fn (string) !, set_polygon_
}

file_menu_button_text_to_action_function_map := create_file_menu_button_text_to_action_function_map(open_sprite_file_function,
create_polygon_file_function, save_polygon_function)
open_polygon_file_function, create_polygon_file_function, save_polygon_function)

file_menu_buttons := create_file_menu_buttons(file_menu_button_text_to_action_function_map)

Expand All @@ -58,8 +66,8 @@ fn create_file_menu(load_sprite_to_viewport_function fn (string) !, set_polygon_
)
}

fn open_sprite_file_dialog_with_kdialog() string {
file_path_selection_result := os.execute("kdialog --getopenfilename . '*.png'")
fn open_file_dialog_with_kdialog(file_extension string) string {
file_path_selection_result := os.execute("kdialog --getopenfilename . '*${file_extension}'")

return file_path_selection_result.output.trim_indent()
}
Expand All @@ -80,11 +88,11 @@ fn open_polygon_file_save_dialog_with_kdialog() !string {
return save_path
}

fn create_file_menu_button_text_to_action_function_map(open_sprite_file_function fn (&ui.MenuItem), create_polygon_file_function fn (&ui.MenuItem), save_polygon_function fn (&ui.MenuItem)) map[string]fn (&ui.MenuItem) {
fn create_file_menu_button_text_to_action_function_map(open_sprite_file_function fn (&ui.MenuItem), open_polygon_file_function fn (&ui.MenuItem), create_polygon_file_function fn (&ui.MenuItem), save_polygon_function fn (&ui.MenuItem)) map[string]fn (&ui.MenuItem) {
unsafe {
return {
'Open sprite file': open_sprite_file_function
'Open polygon file': nil
'Open polygon file': open_polygon_file_function
'Create new polygon': create_polygon_file_function
'Save polygon': save_polygon_function
'Save polygon as...': nil
Expand Down