1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Modularized DiploCorner

Discussion in 'Civ5 - SDK / LUA' started by alpaca, Dec 17, 2010.

  1. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    An improved version of this mod component was created by whoward69. You can find it here

    This is now outdated


    Modularized DiploCorner

    I created a version of diplo corner that allows you to easily add new item to it and should preserve compatibility between mods if they all include this version.

    To use it, just add it to your mod with VFS set to true for both DiploCorner.xml and DiploCorner.lua, then add your own Lua file that has the code for the button you want to add (see the example below) as a DiploCornerAddin in the content tab of your mod properties.

    Resizing is handled automatically so there's no need to do anything much.


    Tutorial

    Step 1: Adding the DiploCorner

    Extract the attached files to wherever your mod resides. Add them to your ModBuddy project by right-clicking on the folder they're in and selecting Add->Add existing item. Select both and click OK.



    Select the files you just added in turn in ModBuddy and make sure to set ImportIntoVFS to True in the bottom right options inspector.



    Step 2: Creating your button

    Create a new Lua file in ModBuddy and give it a name of your choosing (I called it ImprovedNotifications_DiploCornerHook.lua). Make sure the filename you choose is something unique to your mod to avoid incompatibility with other mods, an easy way to do that is to prepend your modname to it. Set ImportIntoVFS to True for this file, too. Open it and put a code like the following into it:

    Code:
    LuaEvents.DiploCornerAddin({ text="To click...", tip="... or not to click", call=function() 
    		print("I am clicked! Woe is me!")
    	end})
    What this file does is to invoke a LuaEvent I defined called DiploCornerAddin. This event takes a table as parameter that contains the data necessary for DiploCorner to create a new button.

    The text property is the text that's displayed on the button. This property can be an index to a localization string, which is in fact recommended, or a string (like in the example).

    The tip property works in the same fashion and allows you to add a tooltip if you so choose. The vanilla buttons don't have tooltips but adding one works flawlessly.

    The more interesting part is the call property, which stores a function that is executed when the user clicks your button. For testing, this just prints a message to your Lua.log (and FireTuner). A slightly more complicated version that I use in the new version of my Notifications mod is this:
    Code:
    LuaEvents.DiploCornerAddin({ text="Notification Options", tip="Opens a window that allows you to choose which notifications you would like to receive.", call=function() 
    		if notificationOptionsContext == nil then
    			notificationOptionsContext = ContextPtr:LoadNewContext("NotificationOptions") 
    		end
    		UIManager:QueuePopup( notificationOptionsContext, PopupPriority.BarbarianCamp ); 
    	end})
    In the first part of the call function, I set up a new Lua Context for my NotificationOptions panel, which is stored in a file called NotificationOptions.lua that is added into my VFS. In the second part I queue it to the UI as a pop-up window.

    The context is only set up the first time the user clicks the button. Afterwards, the old context is reused as a pop-up. Doing it like this allows me to keep local data for that context in the game's memory instead of having to load it again every time the button is clicked.


    Step 3: Adding the DiploCornerAddin

    Now we're almost done. All that remains is to add the DiploCornerAddin to the Content tab, like this:



    Step 4: Testing

    If everything worked correctly, you should now have a new button in the DiploCorner. Click it and your Lua.log should show you a message.


     

    Attached Files:

  2. robk

    robk Chieftain

    Joined:
    Sep 8, 2006
    Messages:
    210
    Location:
    Santa Monica, CA
    Nice! I'll definitely be giving this a try soon. :cool:
     
  3. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    Good, it would return compatibility between my notifications mod and info addict :lol:

    It's really a shame that even things like this aren't written in a modular way out of the box as it wouldn't have been difficult. Button lists and stacks should in theory lend themselves well to just adding or removing things to them via sql-like statements.
     
  4. martijnaikema

    martijnaikema Chieftain

    Joined:
    Oct 30, 2010
    Messages:
    111
    Very good tutorial! It worked the way you described it.
    So you edited the lua and xml so that they are modular the way you described in the tutorial?
     
  5. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    Yes, if multiple mods have this version of DiploCorner.xml and DiploCorner.lua, it doesn't matter which order you load them in - either copy will load all of the DiploCornerAddin content files and place them accordingly. In the screenshot you can see it has both the test mod and the Notification Options button I added, and they are in different mods. The order the buttons appear depends on how the mods are activated, though (I think the mod activated first is further up).

    You can also add multiple buttons in one mod in the same way, just add two lua files. It's not a hugely important thing but I had to edit DiploCorner anyways and noticed that InfoAddict uses it, so I could either make a compatible version of Improved Notifications (because I usually use both mods) or make the file modular and edit my personal copy of InfoAddict to comply.
     
  6. robk

    robk Chieftain

    Joined:
    Sep 8, 2006
    Messages:
    210
    Location:
    Santa Monica, CA
  7. Zyxpsilon

    Zyxpsilon Running Spider

    Joined:
    Oct 29, 2009
    Messages:
    2,951
    Gender:
    Male
    Location:
    On Earth
    Ahhhhh, finally a worthy solution to a tricky & busy corner of the Vanilla UI!
    As a bonus, this precious stuff will also become essential to activate EraCorner(s) or even the planned LeoPaRd context & its weird Notification routines.
    Mucho thank'yo, alpaca!

    PS; AND - now... i can finally update my version of robk's Info_Addict too.
     
  8. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    Excellent work Alpaca. :thumbsup:
    ...and your tutorial is very easy on the eyes. ;)

    I just reimplemented Kael's ModList as an InGameUIAddin, then robk told me of this thread. Version 2 will be a DiploCornerAddin for sure.
     
  9. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    Mod List (addin) Version 2 uploaded and using your DiploCornerAddin standard.
    _
     
  10. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    Alpaca, If you'll take a quick look at the new version of Mod List, you'll notice it is automatically "flexible", loading as a DiploCornerAddin when possible or as an InGameUIAddin otherwise. If you'll notice, I had to hack my own files just a little to make the flexibility possible. Is there a better way to implement this automatic detection without modifying the DiploCorner files?
     
  11. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    I'm not sure what exactly you're trying to do. The problem is that if there is one mod loading it as InGame and another loading as DiploCorner you'll get both?

    My modified DiploCorner actually shouldn't care how you load the file, it just offers the add-in method because I thought it's easier to understand and because I wasn't sure if I wouldn't have to access the add-ins at a later point. It should work just fine if you call the event from a file with a context you load from somewhere else.

    So in your case, it's probably easiest to check Modding.GetActivatedModEntryPoints("DiploCornerAddin") and if there are none, load with the button, otherwise load with the event. You could also try what Modding.GetModEntryPoints does
     
  12. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    It's not a matter of trying, I'm already doing it. :)

    My concern is that the Addin type juggling is based on a bit of an assumption, based on the number of times loaded, not an exact check how how it was loaded. I like your suggestion for Modding.GetActivatedModEntryPoints("DiploCornerAdd in"). I'll look into that for the next version for a cleaner implementation of the auto detection.

    Thanks for the help. I like this standard you've developed and I'm trying to help push it out with Mod List. The problem with the previous version was that in order to use the DiploCornerAddin standard, it risked conflict with mods that also alter the DiploCorner files. Not good for a mod listing mod, you know? So I came up with this auto detect scheme, I just want to make it cleaner and more precise. At first I thought that might require some kind of retrievable flag in the DiploCorner file, but as I suspected, it appears that won't be necessary.
     
  13. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    Alternatively, and more safely, you could get the DiploCorner context and check if the addins variable exists. That is defined as global so you can inspect it from other contexts. Doing that would have the advantage of making sure that your script works even if there are conflicts in some of the mods loaded and somebody inadvertently overrode the DiploCorner with a custom version
     
  14. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    How would I get the DiploCorner context from my lua?
     
  15. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    ContextPtr:LookUpControl( "/InGame/WorldView/DiploCorner" ) should do the trick
     
  16. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    That returns the entire context? I assumed it returned an individual control by id. Sorry, I must be getting lazy. :)
     
  17. Sneaks

    Sneaks Brooklyn Bum

    Joined:
    Oct 15, 2010
    Messages:
    1,877
    Location:
    NYC
    alpaca, where did you get the initial size 92 from when determining the setsizeY? Is the the distance from the screen top to pulldown top or something else?
     
  18. alpaca

    alpaca King of Ungulates

    Joined:
    Aug 3, 2006
    Messages:
    2,322
    I don't tend to memorize such details, sorry. I would have to look at the code again
     
  19. Mylon

    Mylon Amateur Game Designer

    Joined:
    Nov 4, 2005
    Messages:
    1,013
  20. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456

Share This Page