CabinGame/Luxe/blocks/ui.wren
2020-09-12 11:35:28 +02:00

237 lines
No EOL
7.5 KiB
Text

import "luxe: draw" for Draw, PathStyle
import "luxe: world" for Entity, Transform, UI, UIRenderMode
import "luxe: world" for UILayout, UILayoutBehave, UILayoutContain
import "luxe: ui/control" for Control
import "luxe: ui/panel" for UIPanel
import "luxe: ui/list" for UIList
import "luxe: ui/button" for UIButton
import "luxe: ui/image" for UIImage
import "luxe: assets" for Assets
import "luxe: ui/text" for UIText
import "luxe: render" for Material
import "globals" for Globals
import "blocks/ui/image_button" for ImageButton
import "math/observable" for Observable
import "blocks/ui/simple_text" for UISimpleText
class Ui{
static Planning{"planning"}
static Info {"info"}
font{"assets/fonts/BabyBlocks"}
ui_mode{_ui_mode.value}
ui_mode=(v){_ui_mode.value = v}
construct new(app){
var game = Globals["Game"]
var ui_rect = Globals["UiRect"]
_ui_mode = Observable.new(Ui.Info)
_ui = Entity.create(app.ui, "UI Root")
UI.create(_ui, ui_rect.x, ui_rect.y, ui_rect.width, ui_rect.height, 0, app.ui_camera)
UI.set_render_mode(_ui, UIRenderMode.world)
var solid_mat = Material.create("luxe: material_basis/ui_solid")
var text_mat = Material.create("luxe: material_basis/ui_font")
//var text_mat = Material.create("shaders/pixel_text_ui")
UI.set_material_basis(_ui, "luxe: material_basis/ui_solid", "shaders/pixel_text_ui")
UILayout.create(_ui)
_info = setup_info()
_planning = setup_planning()
_ui_mode.on_change(true) {|val|
Control.set_visible(_planning, val == Ui.Planning)
Control.set_visible(_info, val == Ui.Info)
UI.commit(_ui)
}
game.Focus.on_change() {|val|
ui_mode = Ui.Info
}
UI.commit(_ui)
UILayout.commit(_ui)
}
setup_info(){
var ui_rect = Globals["UiRect"]
var root = Control.create(_ui)
UILayout.set_behave(_ui, root, UILayoutBehave.fill)
UILayout.set_margin(_ui, root, 0, 0, 0, 0)
setup_info_toolbar(root)
setup_info_human(root)
return root
}
setup_info_toolbar(root){
var ui_rect = Globals["UiRect"]
//list
var list = Control.create(_ui)
Control.set_id(list, "info list")
Control.child_add(root, list)
//make list span bottom area
UILayout.set_behave(_ui, list, UILayoutBehave.left | UILayoutBehave.right | UILayoutBehave.bottom)
UILayout.set_margin(_ui, list, 0, 0, 0, 0)
UILayout.set_contain(_ui, list, UILayoutContain.row | UILayoutContain.start)
Control.set_size(list, 16, 16)
var adventureButtons = Assets.image("assets/AdventureButtons")
var tiles = [10, 1]
var button
//plan adventure
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "Adventure!")
ImageButton.set_tile_uv(button, tiles, [1, 0])
ImageButton.set_state_change(button) { |data|
if(data["press"]){
ui_mode = Ui.Planning
}
}
//info
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "Stats")
ImageButton.set_tile_uv(button, tiles, [2, 0])
//read diary
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "Diary")
ImageButton.set_tile_uv(button, tiles, [1, 0])
//resources
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "Resources")
ImageButton.set_tile_uv(button, tiles, [4, 0])
}
setup_info_human(root){
var portrait = UIImage.create(_ui)
Control.set_pos(portrait, 1, 1)
Control.set_size(portrait, 30, 46)
Control.set_id(portrait, "person info portrait")
Control.child_add(root, portrait)
UILayout.set_behave(_ui, portrait, UILayoutBehave.left | UILayoutBehave.top)
UILayout.set_margin(_ui, portrait, 1, 1, 0, 0)
var frame = UIImage.create(_ui)
UIImage.set_image(frame, Assets.image("assets/wip/Frame"))
Control.set_pos(frame, 1, 1)
Control.set_size(frame, 30, 46)
Control.set_id(frame, "person info frame")
Control.child_add(portrait, frame)
Globals["Game"].Focus.on_change(true) {|val|
//todo: more sophisticated portrait generation
UIImage.set_image(portrait, Assets.image("assets/wip/Portrait"))
UIImage.set_color(portrait, Human.get_color(val) || [0, 0, 0, 1])
}
var statBlock = Control.create(_ui)
UILayout.set_behave(_ui, statBlock, UILayoutBehave.fill)
UILayout.set_margin(_ui, statBlock, 33, 1, 0, 16)
UILayout.set_contain(_ui, statBlock, UILayoutContain.column | UILayoutContain.start)
var name = UISimpleText.create(_ui)
Control.child_add(statBlock, name)
Control.set_id(name, "human name info")
Globals["Game"].Focus.on_change(true) {|val|
var name_string = Human.get_name(val) || "-"
UISimpleText.set_text(name, name_string)
}
var adventures = UISimpleText.create(_ui)
Control.child_add(statBlock, adventures)
Control.set_id(name, "human adventure count")
Globals["Game"].Focus.on_change(true) {|val|
var count = Human.get_adventure_count(val)
var text = count ? "Adventures: %(count)" : ""
UISimpleText.set_text(adventures, text)
}
}
setup_planning(){
var ui_rect = Globals["UiRect"]
var game = Globals["Game"]
var root = Control.create(_ui)
Control.set_size(root, ui_rect.width, ui_rect.height)
//list
var list = Control.create(_ui)
Control.child_add(root, list)
Control.set_size(list, 0, 16)
UILayout.set_behave(_ui, list, UILayoutBehave.left | UILayoutBehave.right | UILayoutBehave.bottom)
UILayout.set_margin(_ui, list, 0, 0, 0, 0)
UILayout.set_contain(_ui, list, UILayoutContain.row | UILayoutContain.start)
var adventureButtons = Assets.image("assets/AdventureButtons")
var tiles = [10, 1]
var button
//back to info
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "Info")
ImageButton.set_tile_uv(button, tiles, [1, 0])
ImageButton.set_state_change(button) { |data|
if(data["press"]){
ui_mode = Ui.Info
}
}
//abort
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "Abort")
ImageButton.set_tile_uv(button, tiles, [0, 0])
//people
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "People")
ImageButton.set_tile_uv(button, tiles, [2, 0])
//stuff
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "Stuff")
ImageButton.set_tile_uv(button, tiles, [4, 0])
//direction
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "Direction")
ImageButton.set_tile_uv(button, tiles, [3, 0])
//go
button = list_button(list)
UIImage.set_image(button, adventureButtons)
ImageButton.set_tooltip(button, "Depart")
ImageButton.set_tile_uv(button, tiles, [5, 0])
return root
}
list_button(parent){
var button = ImageButton.create(_ui)
Control.set_size(button, 16, 16)
Control.child_add(parent, button)
return button
}
}
//this is behind the class def to make cyclic dependency happy lol
import "blocks/human/human" for Human