2019-06-29 01:47:38 +00:00
use super ::{ img_ids ::Imgs , Fonts , TEXT_COLOR } ;
2019-05-04 10:43:37 +00:00
use conrod_core ::{
2019-05-07 03:25:25 +00:00
color ,
2019-06-29 01:47:38 +00:00
position ::Relative ,
2019-05-07 03:25:25 +00:00
widget ::{ self , Button , Image , Rectangle , Scrollbar } ,
2019-06-29 13:02:27 +00:00
widget_ids , Colorable , Labelable , Positionable , Sizeable , Widget , WidgetCommon ,
2019-05-04 10:43:37 +00:00
} ;
widget_ids! {
struct Ids {
bag_close ,
bag_contents ,
inv_alignment ,
2019-05-06 18:49:12 +00:00
inv_grid_1 ,
inv_grid_2 ,
2019-05-04 10:43:37 +00:00
inv_scrollbar ,
inv_slot_0 ,
map_title ,
2019-06-29 00:11:53 +00:00
inv_slot [ ] ,
2019-06-29 01:47:38 +00:00
item1 ,
2019-05-04 10:43:37 +00:00
}
}
#[ derive(WidgetCommon) ]
pub struct Bag < ' a > {
2019-06-29 00:11:53 +00:00
inventory_space : usize ,
2019-05-04 10:43:37 +00:00
imgs : & ' a Imgs ,
2019-07-02 21:25:07 +00:00
_fonts : & ' a Fonts ,
2019-05-04 10:43:37 +00:00
#[ conrod(common_builder) ]
common : widget ::CommonBuilder ,
}
impl < ' a > Bag < ' a > {
2019-06-29 00:11:53 +00:00
pub fn new ( inventory_space : usize , imgs : & ' a Imgs , fonts : & ' a Fonts ) -> Self {
2019-05-04 10:43:37 +00:00
Self {
inventory_space ,
imgs ,
2019-07-02 21:25:07 +00:00
_fonts ,
2019-05-04 10:43:37 +00:00
common : widget ::CommonBuilder ::default ( ) ,
}
}
}
pub struct State {
ids : Ids ,
}
pub enum Event {
Close ,
}
impl < ' a > Widget for Bag < ' a > {
type State = State ;
type Style = ( ) ;
type Event = Option < Event > ;
fn init_state ( & self , id_gen : widget ::id ::Generator ) -> Self ::State {
State {
ids : Ids ::new ( id_gen ) ,
}
}
fn style ( & self ) -> Self ::Style {
( )
}
fn update ( self , args : widget ::UpdateArgs < Self > ) -> Self ::Event {
2019-05-07 05:40:03 +00:00
let widget ::UpdateArgs { state , ui , .. } = args ;
2019-05-04 10:43:37 +00:00
// Contents
Image ::new ( self . imgs . bag_contents )
2019-05-06 18:49:12 +00:00
. w_h ( 68.0 * 4.0 , 123.0 * 4.0 )
. bottom_right_with_margins_on ( ui . window , 60.0 , 5.0 )
2019-05-04 10:43:37 +00:00
. set ( state . ids . bag_contents , ui ) ;
// Alignment for Grid
2019-05-06 18:49:12 +00:00
Rectangle ::fill_with ( [ 58.0 * 4.0 - 5.0 , 100.0 * 4.0 ] , color ::TRANSPARENT )
2019-05-07 05:40:03 +00:00
. top_left_with_margins_on ( state . ids . bag_contents , 11.0 * 4.0 , 5.0 * 4.0 )
. scroll_kids ( )
. scroll_kids_vertically ( )
. set ( state . ids . inv_alignment , ui ) ;
2019-05-04 10:43:37 +00:00
// Grid
Image ::new ( self . imgs . inv_grid )
2019-05-07 05:40:03 +00:00
. w_h ( 58.0 * 4.0 , 111.0 * 4.0 )
. mid_top_with_margin_on ( state . ids . inv_alignment , 0.0 )
. set ( state . ids . inv_grid_1 , ui ) ;
Image ::new ( self . imgs . inv_grid )
. w_h ( 58.0 * 4.0 , 111.0 * 4.0 )
. mid_top_with_margin_on ( state . ids . inv_alignment , 110.0 * 4.0 )
. set ( state . ids . inv_grid_2 , ui ) ;
Scrollbar ::y_axis ( state . ids . inv_alignment )
. thickness ( 5.0 )
. rgba ( 0.33 , 0.33 , 0.33 , 1.0 )
. set ( state . ids . inv_scrollbar , ui ) ;
2019-06-29 13:03:48 +00:00
// Create available inventory slot widgets
2019-06-29 13:02:27 +00:00
if state . ids . inv_slot . len ( ) < self . inventory_space {
2019-06-29 13:03:48 +00:00
state . update ( | s | {
s . ids
. inv_slot
. resize ( self . inventory_space , & mut ui . widget_id_generator ( ) ) ;
} ) ;
}
2019-06-29 13:02:27 +00:00
// "Allowed" max. inventory space should be handled serverside and thus isn't limited in the UI
2019-06-29 00:11:53 +00:00
for i in 0 .. self . inventory_space {
let x = i % 5 ;
2019-06-29 13:03:48 +00:00
let y = i / 5 ;
2019-05-07 03:25:25 +00:00
Button ::image ( self . imgs . inv_slot )
2019-06-29 13:03:48 +00:00
. top_left_with_margins_on (
state . ids . inv_grid_1 ,
4.0 + y as f64 * ( 40.0 + 4.0 ) ,
4.0 + x as f64 * ( 40.0 + 4.0 ) ,
) // conrod uses a (y,x) format for placing...
2019-06-29 13:02:27 +00:00
. parent ( state . ids . inv_grid_2 ) // Avoids the background overlapping available slots
2019-06-29 13:03:48 +00:00
. w_h ( 40.0 , 40.0 )
2019-06-29 00:11:53 +00:00
. set ( state . ids . inv_slot [ i ] , ui ) ;
2019-06-29 13:03:48 +00:00
}
2019-06-29 01:47:38 +00:00
// Test Item
if self . inventory_space > 0 {
2019-06-29 13:03:48 +00:00
Button ::image ( self . imgs . potion_red ) // TODO: Insert variable image depending on the item displayed in that slot
. w_h ( 4.0 * 3.5 , 7.0 * 3.5 ) // TODO: height value has to be constant(but not the same as the slot widget), while width depends on the image displayed to avoid stretching
2019-06-29 13:02:27 +00:00
. middle_of ( state . ids . inv_slot [ 0 ] ) // TODO: Items need to be assigned to a certain slot and then placed like in this example
. label ( " 5x " ) // TODO: Quantity goes here...
2019-06-29 01:47:38 +00:00
. label_font_id ( self . fonts . opensans )
. label_font_size ( 12 )
2019-06-29 13:03:48 +00:00
. label_x ( Relative ::Scalar ( 10.0 ) )
2019-06-29 01:47:38 +00:00
. label_y ( Relative ::Scalar ( - 10.0 ) )
2019-06-29 13:03:48 +00:00
. label_color ( TEXT_COLOR )
2019-06-29 13:02:27 +00:00
. set ( state . ids . item1 , ui ) ; // TODO: Add widget_id generator for displayed items
2019-06-29 01:47:38 +00:00
}
2019-05-04 10:43:37 +00:00
// X-button
if Button ::image ( self . imgs . close_button )
2019-05-06 18:49:12 +00:00
. w_h ( 28.0 , 28.0 )
2019-05-04 10:43:37 +00:00
. hover_image ( self . imgs . close_button_hover )
. press_image ( self . imgs . close_button_press )
2019-05-06 18:49:12 +00:00
. top_right_with_margins_on ( state . ids . bag_contents , 0.0 , 0.0 )
2019-05-04 10:43:37 +00:00
. set ( state . ids . bag_close , ui )
. was_clicked ( )
{
2019-05-07 03:25:25 +00:00
Some ( Event ::Close )
} else {
None
2019-05-04 10:43:37 +00:00
}
}
}