//========================================================================================= /// <summary> /// Gui event. Happens when brightness slider is moved. /// </summary> /// <param name="widget"> Widget that the event is happening on. </param> //========================================================================================= public static void Event_Brightness_Changed( GuiWidget widget ) { // See if this is a slider: if ( widget != null ) { try { // Cast to a slider: Gui_Slider s = (Gui_Slider) widget; // Set brightness: CorePreferences.Brightness = s.CurrentValue; } catch ( Exception ){} } }
//========================================================================================= /// <summary> /// Gui event. Happens when music volume slider is moved. /// </summary> /// <param name="widget"> Widget that the event is happening on. </param> //========================================================================================= public static void Event_Music_Volume_Changed( GuiWidget widget ) { // See if this is a slider: if ( widget != null ) { try { // Cast to a slider: Gui_Slider s = (Gui_Slider) widget; // Set music volume: CorePreferences.MusicVolume = s.CurrentValue; } catch ( Exception ){} } }
//========================================================================================= /// <summary> /// Gui event. Sets slider values for the options menu. /// </summary> /// <param name="widget"> Widget that the event is happening on. </param> //========================================================================================= public static void Event_Set_Options_Sliders( GuiWidget widget ) { // Set all settings: try { // Try to find this object: Gui_Slider s = (Gui_Slider) Core.Gui.Search.FindByName("Slider_Brightness"); // Set it's value: if ( s != null ) s.CurrentValue = CorePreferences.Brightness; } catch ( Exception ){} try { // Try to find this object: Gui_Slider s = (Gui_Slider) Core.Gui.Search.FindByName("Slider_Music_Volume"); // Set it's value: if ( s != null ) s.CurrentValue = CorePreferences.MusicVolume; } catch ( Exception ){} try { // Try to find this object: Gui_Slider s = (Gui_Slider) Core.Gui.Search.FindByName("Slider_Sound_Volume"); // Set it's value: if ( s != null ) s.CurrentValue = CorePreferences.SoundVolume; } catch ( Exception ){} }
//========================================================================================= /// <summary> /// Gui event. Opens up the previously open screen. /// </summary> /// <param name="widget"> Widget that the event is happening on. </param> //========================================================================================= public static void Event_Pop_Screen( GuiWidget widget ) { // Load the previous screen: Core.Gui.PopScreen(); }
//========================================================================================= /// <summary> /// Chooses the current widget that is to be in focus. Picks the first widget that has the /// 'DefaultFocus' flag set to true, or if that fails the first focusable widget. /// </summary> //========================================================================================= private void ChooseFocusWidget() { // Lock the objects list: m_data.Lock(); // Clear the current focus widget: m_focus_widget = null; // Run through the list of widgets and try to find a default focus one: Dictionary<int,GuiWidget>.Enumerator e = m_data.Objects.GetEnumerator(); while ( e.MoveNext() ) { // See if this is a default focus widget: if ( e.Current.Value.DefaultFocus ) { // Found our focus widget: set m_focus_widget = e.Current.Value; // Call the gained focus event: m_focus_widget.OnFocusGained(); // Abort search m_data.Unlock(); return; } } // Ok: run through the list of widgets that can have focus and try to pick one e = m_data.Objects.GetEnumerator(); while ( e.MoveNext() ) { // See if this is a default focus widget: if ( e.Current.Value.CanFocus ) { // Found our focus widget: set m_focus_widget = e.Current.Value; // Call the gained focus event: m_focus_widget.OnFocusGained(); // Abort search m_data.Unlock(); return; } } // Unlock the objects list: m_data.Unlock(); }
//========================================================================================= /// <summary> /// Makes focus shift in the direction specified if allowed. /// </summary> //========================================================================================= public void TraverseFocus( FocusTraversalDirection direction ) { // See if there is a focus widget: we can do nowt if there is none if ( m_focus_widget != null ) { // Ok: see what direction we have to move in switch ( direction ) { // Move focus to the left: case FocusTraversalDirection.LEFT: { // Try to find the next widget to focus on: GuiWidget next_focus_widget = m_search.FindByName( m_focus_widget.LeftWidget ); // If found then switch focus: if ( next_focus_widget != null ) { // Switch focus: m_focus_widget.OnFocusLost(); m_focus_widget = next_focus_widget; // Call focus gained on new widget: m_focus_widget.OnFocusGained(); } } break; case FocusTraversalDirection.RIGHT: { // Try to find the next widget to focus on: GuiWidget next_focus_widget = m_search.FindByName( m_focus_widget.RightWidget ); // If found then switch focus: if ( next_focus_widget != null ) { // Switch focus: m_focus_widget.OnFocusLost(); m_focus_widget = next_focus_widget; // Call focus gained on new widget: m_focus_widget.OnFocusGained(); } } break; case FocusTraversalDirection.ABOVE: { // Try to find the next widget to focus on: GuiWidget next_focus_widget = m_search.FindByName( m_focus_widget.AboveWidget ); // If found then switch focus: if ( next_focus_widget != null ) { // Switch focus: m_focus_widget.OnFocusLost(); m_focus_widget = next_focus_widget; // Call focus gained on new widget: m_focus_widget.OnFocusGained(); } } break; case FocusTraversalDirection.BELOW: { // Try to find the next widget to focus on: GuiWidget next_focus_widget = m_search.FindByName( m_focus_widget.BelowWidget ); // If found then switch focus: if ( next_focus_widget != null ) { // Switch focus: m_focus_widget.OnFocusLost(); m_focus_widget = next_focus_widget; // Call focus gained on new widget: m_focus_widget.OnFocusGained(); } } break; } // end switch ( direction ) } }
//========================================================================================= /// <summary> /// Clears the gui. This should be used as opposed to manually clearing gui data /// when a level-wipe is required. /// </summary> //========================================================================================= public void Clear() { // Recreate all objects and override the old ones: m_data = new GuiData(this); m_search = new GuiSearchQuery(this); // Clear the curretnly focused widget: m_focus_widget = null; }
//========================================================================================= /// <summary> /// Attempts to invoke a given generic gui event function. The widget involved in the event /// must be given or else this function will do nothing. /// </summary> /// < /// <param name="event_widget"> Widget involved in the event. </param> //========================================================================================= public static void InvokeGenericEvent( string event_name , GuiWidget event_widget ) { // If no widget is given then abort: if ( event_widget == null ) return; // See if this event exists: if ( s_generic_events.ContainsKey(event_name) ) { // Ok: get the event: OnGuiGenericEventFunction event_function = s_generic_events[event_name]; // Cool: run the event. Catch any exceptions that happen try { event_function(event_widget); } // If something went wrong then show it on windows debug #if WINDOWS_DEBUG catch ( Exception e ){ DebugConsole.PrintException(e); } #else catch ( Exception ){} #endif } }
//========================================================================================= /// <summary> /// Registers an objects name with the gui so the object can later be found by using this /// name. The name does not have to be unique and multiple objects could be retrieved under /// the same name. /// </summary> /// /// <param name="obj"> Object to register name for </param> /// <param name="name"> Name to register the object under </param> //========================================================================================= public void RegisterName( GuiWidget obj , string name ) { // Abort if null object: if ( obj == null ) return; // Do nothing if the object has no ID or is not in the gui: if ( obj.Id == 0 || obj.ParentContainer != this || obj.ParentGui != this.m_gui ) return; // Do nothing if no name was given: if ( name == null || name.Length <= 0 ) return; // If the gui data is locked then just add to the list of pending gui data changes: if ( m_lock_count != 0 ) { // Add this change to the list: m_pending_changes.AddLast ( new PendingGuiChange ( obj , PendingGuiChangeType.REGISTER_NAME , name ) ); // Abort: do not actually add the object into the list - we will do this later on Unlock() return; } // Make the name lowercase: string nameLower = name.ToLower(); // Create a hash for objects with this name if not already in existance if ( m_names.ContainsKey( nameLower ) == false ) { m_names.Add( nameLower , new Dictionary<int,GuiWidget>() ); } // Add the object into the hash: m_names[nameLower][obj.Id] = obj; }
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /// <summary> /// Constructor for the pending gui change structure. /// </summary> /// <param name="o"> Object in question. </param> /// <param name="t"> Type of level change. </param> /// <param name="n"> Name of the object at type of change. For name changes only!</param> //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' public PendingGuiChange( GuiWidget o , PendingGuiChangeType t , string n ) { obj = o; changeType = t; objectName = n; }
//========================================================================================= /// <summary> /// Unregisters a widgets's type name with the scene. After this has finished the object will /// no longer be found when searching for objects with this type name. /// </summary> /// /// <param name="obj"> Object to unregister name for </param> //========================================================================================= private void UnregisterType( GuiWidget obj ) { // Abort if null object: if ( obj == null ) return; // Do nothing if the object has no ID or is not in the gui: if ( obj.Id == 0 || obj.ParentContainer != this || obj.ParentGui != this.m_gui ) return; // Get the object's type name: string typeName = obj.GetType().Name; // Make sure the hash contains the type name if ( m_types.ContainsKey( typeName ) ) { // Get the hash for this type name: Dictionary<int,GuiWidget> hash = m_types[typeName]; // See if this object exists in the hash if ( hash.ContainsKey( obj.Id ) ) { // Bingo: remove the object from the hash hash.Remove( obj.Id ); // If the hash is now empty then remove it from the parent hash if ( hash.Count == 0 ){ m_types.Remove( typeName ); } } } }
//========================================================================================= /// <summary> /// Registers a widgets type name with the gui data so the object can later be found by /// using this type name. Multiple objects can be registered with the same type. /// </summary> /// /// <param name="obj">Object to register type name for</param> //========================================================================================= private void RegisterType( GuiWidget obj ) { // Abort if null object: if ( obj == null ) return; // Do nothing if the object has no ID or is not in the gui: if ( obj.Id == 0 || obj.ParentContainer != this || obj.ParentGui != this.m_gui ) return; // Get the object's type name: string typeName = obj.GetType().Name; // Create a hash for widgets with this type if not already in existance if ( m_types.ContainsKey( typeName ) == false ) { m_types.Add( typeName , new Dictionary<int,GuiWidget>() ); } // Add the object into the hash: m_types[typeName][obj.Id] = obj; }
//========================================================================================= /// <summary> /// Unregisters an object's name with the gui. After this has finished the object will /// no longer be found when searching for objects with this name. /// </summary> /// /// <param name="obj"> Object to unregister name for </param> /// <param name="name"> Name that the object is registered under </param> //========================================================================================= public void UnregisterName( GuiWidget obj , string name ) { // Abort if null object: if ( obj == null ) return; // Do nothing if the object has no ID or is not in the scene: if ( obj.Id == 0 || obj.ParentContainer != this || obj.ParentGui != this.m_gui ) return; // Do nothing if no name was given: if ( name == null || name.Length <= 0 ) return; // If the level data is locked then just add to the list of pending gui data changes: if ( m_lock_count != 0 ) { // Add this change to the list: m_pending_changes.AddLast ( new PendingGuiChange ( obj , PendingGuiChangeType.UNREGISTER_NAME , name ) ); // Abort: do not actually add the object into the list - we will do this later on Unlock() return; } // Make the name lowercase: string nameLower = name.ToLower(); // Make sure the hash contains the name if ( m_names.ContainsKey( nameLower ) ) { // Get the hash for this name: Dictionary<int,GuiWidget> hash = m_names[nameLower]; // See if this widget exists in the hash if ( hash.ContainsKey( obj.Id ) ) { // Bingo: remove the widget from the hash hash.Remove( obj.Id ); // If the hash is now empty then remove it from the parent hash if ( hash.Count == 0 ){ m_names.Remove( nameLower ); } } } }
//========================================================================================= /// <summary> /// Removes the given widget from the gui. /// </summary> /// /// <param name="obj"> Widget to remove</param> //========================================================================================= public void Remove( GuiWidget obj ) { // Do nothing if objet is null: if ( obj == null ) return; // Do nothing if the object has no ID or is not in the gui: if ( obj.Id == 0 || obj.ParentContainer != this || obj.ParentGui != this.m_gui ) return; // See if object exists in gui: if ( m_objects.ContainsKey( obj.Id ) ) { // If the gui data is locked then just add to the list of pending gui data changes: if ( m_lock_count != 0 ) { // Add this change to the list: m_pending_changes.AddLast ( new PendingGuiChange ( obj , PendingGuiChangeType.REMOVE_OBJECT , obj.Name ) ); // Abort: do not actually add the object into the list - we will do this later on Unlock() return; } // Cleanup after the object: obj.OnDelete(); // Unregister object name and type: UnregisterName(obj,obj.Name); UnregisterType(obj); // Remove widget from the master dictionary: m_objects.Remove( obj.Id ); // Add this id to the list of free ids m_free_ids.AddLast(obj.Id); // Clear all the object's scene related variables obj.Id = 0; obj.ParentGui = null; obj.ParentContainer = null; } }
//========================================================================================= /// <summary> /// Gui event. Causes the game to end. /// </summary> /// <param name="widget"> Widget that the event is happening on. </param> //========================================================================================= public static void Event_Quit( GuiWidget widget ) { Core.Game.Exit(); }
//========================================================================================= /// <summary> /// Gui event. Restarts the level. /// </summary> /// <param name="widget"> Widget that the event is happening on. </param> //========================================================================================= public static void Event_Restart_Level( GuiWidget widget ) { // Reload the level: Core.Level.Load( Core.Level.FileName ); }
//========================================================================================= /// <summary> /// Adds a given widget into the gui data block. /// </summary> /// /// <param name="obj"> Widget to add </param> //========================================================================================= public void Add( GuiWidget obj ) { // Make sure object not in another scene: if ( obj.Id != 0 || obj.ParentGui != null || obj.ParentContainer != null ) return; // If the gui data is locked then just add to the list of pending gui data changes: if ( m_lock_count != 0 ) { // Add this change to the list: m_pending_changes.AddLast ( new PendingGuiChange ( obj , PendingGuiChangeType.ADD_OBJECT , obj.Name ) ); // Abort: do not actually add the object into the list - we will do this later on Unlock() return; } // Choose an id for the object: int id = m_next_id; // If there are any free ids from deletion then use them: otherwise use the next free id and increment it if ( m_free_ids.Count > 0 ) { // Use the next free id gotten from deletion: id = m_free_ids.First.Value; m_free_ids.RemoveFirst(); } else { // No id's ready from deletion: use the next free id and increment it m_next_id++; } // Set ID, parent gui and container: obj.Id = id; obj.ParentGui = this.m_gui; obj.ParentContainer = this; // Add to the dictionary for all widgets: m_objects.Add( obj.Id , obj ); // Register the widgets name and type for later quick lookup RegisterName(obj,obj.Name); RegisterType(obj); }