Gtk+ and Vala: Inheriting gtk.TreeModel - gtk

I have a hash with values that build into following structure:
string type1_name -> Hash(
string name_member -> DataStruct,
string name_member -> DataStruct,
string name_member -> DataStruct,
string name_member -> DataStruct
),
string type2_name -> Hash(
string name_member -> DataStruct,
string name_member -> DataStruct,
string name_member -> DataStruct,
string name_member -> DataStruct
),
/// etc
the problem is I have 3 views: 2 TreeViews (extended actual tree showing all columns in hierarchial way and brief as ListStore showing only type icon and name of DataStruct, those views are owned by different parents and maybe shown at the same time) and 1 ListView with icon representation of same data. ATM I have a class managing data (validations, serialization, deserialization, ...) and 3 models for each of those views, so every time I update/delete/create item I must update all 3 models and that doesn't look good :(
What I thought about is creating a class implementing Gtk.TreeModel and providing common data source that can be used as model for all 3 views, but I can't find any documentation on how to implement gtk.TreeModel. I've tried to look through GtkListStore (native C implementaion) and I see it reimplements really lots of methods. Isn't there any easier way?

No, there isn't an easier way, but it's really not too difficult. Looking at the C code can be intimidating, but there are really only about a dozen methods which you need to implement. They are marked as abstract in gtk+-3.0.vapi (and in valadoc.org's Gtk.TreeModel docs)., and the implementations are usually pretty trivial. If you want an example SQLHeavyGtk.Model is the only one I can think of.
The methods which are marked as virtual don't generally require an implementation, though you can provide one if you want (often used for optimizations, and I'm guessing Gtk.ListStore provides a lot of them).

Simple inherit from Gtk.TreeStore / Gtk.ListStore and set the column types explicit.
The tree view column is set to the cell renderer and the data callback function you like to show.
Vala sample to map MyCommon.data1 to the treeview1.column = 0
public class MyCommon : GLib.Object {
string data1 {get; set; }
string data2 {get; set; }
}
public class Store : Gtk.TreeStore {
public Store() {
set_column_types( new GLib.Type[] { typeof(MyCommon) } );
}
public set_treeview1(Gtk.TreeView treeview) {
treeview.insert_column_with_data_func(0, "data1", new Gtk.CellRendererText(), tree_cell_data1);
treeview.insert_column_with_data_func(1, "data2", new Gtk.CellRendererText(), tree_cell_data2);
treeview.model = this;
}
protected MyCommon? my_common(Gtk.TreeModel model, Gtk.TreeIter iter) {
GLib.Value data;
model.get_value(iter, 0, out data);
return (MyCommon)data;
}
public void tree_cell_data1(Gtk.TreeViewColumn column, Gtk.CellRenderer cell,
Gtk.TreeModel model, Gtk.TreeIter iter) {
MyCommon? property = my_common(this,iter);
if(property != null) (cell as Gtk.CellRendererText).text = property.data1;
}
public void tree_cell_data2(Gtk.TreeViewColumn column, Gtk.CellRenderer cell,
Gtk.TreeModel model, Gtk.TreeIter iter) {
MyCommon? property = my_common(this,iter);
if(property != null) (cell as Gtk.CellRendererText).text = property.data2;
}
.....

Related

Class Design - Object Oriented Programming Question

This was asked during an interview.
There are different manufacturers of buses. Each bus has got different models and each model has only 2 variants. So different manufacturers have different models with only 2 variants. The interviewer asked me to design a standalone program with just classes. She mentioned that I should not think about databases and I didn't have to code them. For example, it could be a console based program with inputs and outputs.
The manufacturers, models and variants information should be held in memory (hard-coded values were fine for this standalone program). She wanted to observe the classes and my problem solving approach.
She told me to focus on implementing three APIs or methods for this system.
The first one was to get information about a particular bus. Input would be manufacturer name, model name and variant name. Given these three values, the information about a particular bus such as its price, model, year, etc should be shown to the client.
The second API would be to compare two buses and the output would be to list the features side by side, probably in a tabular format. Input would be the same as the one for the first API i.e. manufacturer name, model name and variant name for both the buses.
The third one would be to search the buses by price (>= price) and get the list of buses which satisfy the condition.
She also added that the APIs should be scalable and I should design the solution with this condition on my mind.
This is how I designed the classes:
class Manufacturer {
private String name;
private Set<Model> models;
// some more properties related to manufacturer
}
class Model {
private String name;
private Integer year;
private Set<Variant> variants;
// some more properties related to model
}
class Variant {
private String name;
private BigDecimal price;
// some more properties related to variant
}
class Bus {
private String manufacturerName;
private String modelName;
private String variantName;
private Integer year;
private BigDecimal price;
// some more additional properties as required by client
}
class BusService {
// The first method
public Bus getBusInformation(String manufacturerName, String modelName, String variantName) throws Exception {
Manufacturer manufacturer = findManufacturer(manufacturerName);
//if(manufacturer == null) throw a valid exception
Model model = findModel(manufacturer);
// if(model == null) throw a valid exception
Variant variant = findVariant(model);
// if(variant == null) throw a valid exception
return createBusInformation(manufacturer, model, variant);
}
}
She stressed that there were only 2 variants and there wouldn't be any more variants and it should be scalable. After going through the classes, she said she understood my approach and I didn't have to implement the other APIs/methods. I realized that she wasn't impressed with the way I designed them.
It would be helpful to understand the mistake I made so that I could learn from it.
I interpreted your 3 requirements a bit differently (and I may be wrong). But it sounds like the overall desire is to be able to perform different searches against all Models, correct?
Also, sounds to me that as all Variants are Models. I suspect different variants would have different options, but nothing to confirm that. If so, a variant is just a subclass of a particular model. However, if variants end up having the same set of properties, then variant isn't anything more than an additional descriptor to the model.
Anyway, going on my suspicions, I'd have made Model the center focus, and gone with:
(base class)
abstract class Model {
private Manufacturer manufacturer;
private String name;
private String variant;
private Integer year;
private BigDecimal price;
// some more properties related to model
}
(manufacturer variants)
abstract class AlphaModel {
AlphaModel() {
this.manufacturer = new Manufacturer() { name = "Alpha" }
}
// some more properties related to this manufacturer
}
abstract class BetaModel {
BetaModel() {
this.manufacturer = new Manufacturer() { name = "Beta" }
}
// some more properties related to this manufacturer
}
(model variants)
abstract class AlphaBus : AlphaModel {
AlphaBus() {
this.name = "Super Bus";
}
// some more properties related to this model
}
abstract class BetaTruck : BetaModel {
BetaTruck() {
this.name = "Big Truck";
}
// some more properties related to this model
}
(actual instances)
class AlphaBusX : AlphaBus {
AlphaBusX() {
this.variant = "X Edition";
}
// some more properties exclusive to this variant
}
class AlphaBusY : AlphaBus {
AlphaBusY() {
this.variant = "Y Edition";
}
// some more properties exclusive to this variant
}
class BetaTruckF1 : BetaTruck {
BetaTruckF1() {
this.variant = "Model F1";
}
// some more properties exclusive to this variant
}
class BetaTruckF2 : BetaTruck {
BetaTruckF2() {
this.variant = "Model F2";
}
// some more properties exclusive to this variant
}
Then finally:
var data = new Set<Model> {
new AlphaBusX(),
new AlphaBusY(),
new BetaTruckF1(),
new BetaTruckF2()
}
API #1:
var result = data.First(x => x.manufacturer.name = <manufactuer>
&& x.name = <model>
&& x.variant = <variant>);
API #2:
var result1 = API#1(<manufacturer1>, <model1>, <variant1>);
var result2 = API#1(<manufacturer2>, <model2>, <variant2>);
API #3:
var result = data.Where(x => x.price >= <price>);
I would say your representation of the Bus class is severely limited, Variant, Model, Manufacturer should be hard links to the classes and not strings. Then a get for the name of each.
E.G from the perspective of Bus bus1 this.variant.GetName() or from the outside world. bus1.GetVariant().name
By limiting your bus to strings of it's held pieces, you're forced to do a lookup even when inside the bus class, which performs much slower at scale than a simple memory reference.
In terms of your API (while I don't have an example), your one way to get bus info is limited. If the makeup of the bus changes (variant changes, new component classes are introduced), it requires a decent rewrite of that function, and if other functions are written similarly then all of those two.
It would require some thought but a generic approach to this that can dynamically grab the info based on the input makes it easier to add/remove component pieces later on. This will be the are your interviewer was focusing on most in terms of advanced technical&language skills. Implementing generics, delegates, etc. here in the right way can make future upkeep of your API a lot easier. (Sorry I don't have an example)
I wouldn't say your approach here is necessarily bad though, the string member variables are probably the only major issue.

Unity-How to Create a Dialogue Tree?

So I have this program set up for a simple dialogue tree, where I want to display a question and two options in the unity editor, and if you click one option, you either go to another level of the tree or a leaf. I want to use the composite design pattern to make separate level instances, each with different parameters for one question and two options and add them together into a list. What I'm stuck on is how do I start at the first level and traverse down the tree depending on which button I press. It seems like no matter what I do, it only displays the last level parameters added to the list. The best I can think is maybe add some sort of shifting list function during the button click events. If anyone could shoot some ideas, I would appreciate it. Thank you.
public class Level : MonoBehaviour {
bool button1Pressed;
bool button2Pressed;
private void Start()
{
Level Level1 = new Level("Hello", "Hi", "Shut Up");
Level leaf1 = new Level("Don't be Rude");
Level Level2 = new Level("What you Doing?", "Not Much", "None of your Business");
Level leaf2 = new Level("Well Excuuuuse Me");
Level Level3 = new Level("Can I do that too?", "Sure", "Go Away");
Level leaf3 = new Level("Fine. Be a Jerk");
Level Level4 = new Level("This is boring, can we do something else?", "Why not?", "You're boring");
Level leaf4 = new Level("I'll go be boring somewhere else");
Level Level5 = new Level("You want ice cream?", "Sounds Good", "I'm allergic");
Level leaf5 = new Level("ok.......");
Level leaf = new Level("I Want Chocolate");
Level1.add(Level1);
Level1.add(leaf1);
Level2.add(Level3);
Level2.add(leaf2);
Level3.add(Level4);
Level3.add(leaf3);
Level4.add(Level5);
Level4.add(leaf4);
Level5.add(leaf5);
Level5.add(leaf);
}
public static Text Textbox;
public static Button Button1;
public static Button Button2;
public string OptionA;
public string OptionB;
public string Question;
public string Leaf;
private List<Level> levels;
public Level(string question, string optionA, string optionB)
{
this.Question = question;
this.OptionA = optionA;
this.OptionB = optionB;
GameObject.FindGameObjectWithTag("Level").GetComponentInChildren<Text>().text = Question;
GameObject.FindGameObjectWithTag("OptionA").GetComponentInChildren<Text>().text = OptionA;
GameObject.FindGameObjectWithTag("OptionB").GetComponentInChildren<Text>().text = OptionB;
levels = new List<Level>();
}
public Level(string leaf)
{
this.Leaf = leaf;
Textbox.text = leaf;
}
public void add(Level lvl)
{
levels.Add(lvl);
}
public List<Level> getLevels()
{
return levels;
}
public void Button1Pressed()
{
}
public void Button2Pressed()
{
}
}
public class Initializer : MonoBehaviour {
public Text Textbox;
public Button Button1;
public Button Button2;
void Awake()
{
Level.Textbox = this.Textbox;
Level.Button1 = this.Button1;
Level.Button2 = this.Button2;
}
}
The short answer: All nodes know their parent and children.
There are a few ways to approach this problem. I'll explain an approach using a tree structure with multiple node classes. First we can examine the interactions with the player as you've outlined:
Making a dialog choice (speaking)
Observing a dialog response (listening)
There's also some important conditions we need to consider:
User terminated conversations
AI terminated conversations
From this outline we can build our tree with some classes. I've scratched out some examples but I haven't tested them. Hopefully it conveys the idea and you can build your own solution. It may also be more useful for you to make a single Node class that just knows which type it is. Another improvement would be using an interface or some way to generalize the parent/child relationship which would allow for more complicated tree structures.
class ChoiceNode
{
public ChoiceNode(ResponseNode myParent)
{
parent = myParent;
}
ResponseNode parent = null;
List<ResponseNode> children = new List<ResponseNode>;
bool canSayGoodbye = true;
}
class ResponseNode
{
public ResponseNode(ChoiceNode myParent, string myMessage)
{
parent = myParent;
parent.children.Add(this);
response = myMessage;
}
ChoiceNode parent;
ChoiceNode child;
string response;
}
We should now be able to use a method to display dialog choice by simply enumerating the ResponseNode.children. Then whenever we make a dialog choice we want to display the ResponseNode.response and then move to the ResponseNode.child to find the next set of dialog choices. When parent == null we're at the root branch. When child == null we display some termination text.
I hope this is helpful and gives you some ideas.
Ok, I tried to understand the logic of your code but there are several things that I don't understand, maybe it's better if I try to explain my "Unity dialogue tree".
First:
We need to create a Tree object. If you just want a binary tree you just need tree variables:
public class BinaryTree{
private string root;
private BinaryTree left;
private BinaryTree right
getters/setters
}
this object don't even need to be a Unity component.
root is "dialog"
left is "optionA"
right is "optionB"
if you don't need multiple answer just make left=right.
If you need a way to identify multiple answer I suggest you to create an object like this:
public class Tree{
private Dictionary<string,string> root;
private List<Tree> next;
getters/setters
}
root is again your dialog. A key (that identify your answer) and a value that is the actual dialog.
next is a List of Tree (you identify your answer by making a loop in your next and by checking the key of the dictionary).
Now in the Start method you need to create a new Tree object and set your nexts.
Example
Start(){
BinaryTree bn = new BinaryTree();
bn.Root = "Is this a question?";
BinaryTree left = new BinaryTree();
left.Root = "Nope";
bn.Left = left;
BinaryTree right = new BinaryTree();
right.Root = "Yes it is...";
bn.Right = right;
}
Almost the same for the Tree version:
Start(){
Tree bn = new Tree();
bn.Root = new Dictionary<string, string>();
bn.Root.Add("key1", "Do you need something?");
bn.Next = new List<Tree>();
Tree answer1 = new Tree();
answer1.Root = new Dictionary<string, string>();
answer1.Root.Add("key2", "Yes");
bn.Next.Add();
... iterate...
}
Of course this is just a basic example. The best way to initialize that is to add your dialog to an array and iterate.
Anyway.
Now you can (for example) create a button. In the Start you can Initialize it's text value to your root. In the PointerDown/Clicked method you can make an array of the possible answer keys and, for example, you can decide to generate multiple buttons for multiple answers (or just use 2 static button answer with the BinaryTree) and change the text based on the answer root value (or left/right value). each of the answer button should, in the PointerDown/Clicked method, send the key value (or left/right object) of your user selection (in practice the next value that will display in the main question button).
Of course the "question button" once clicked again should display the next of your answer (and maybe you can decide to add a bool question variable to your object... or maybe you can just decide to use only the left side for a question... or maybe you can decide that if there is only 1 value in the next List that value is a question and it should just show it in the main button text value).
And if the next is null of course you can end the conversation.
There are multiple ways to do this.

string hints in different languages

I am creating a game in Unity3d.
Somewhere I have hints appearing in the bottom of the HUD (Something like "press A for Action"). I want my game to support more languages and I don't want these hints to be hard-coded in the script.
What is the most elegant way to solve this task? I am thinking about txt file, where I will have all my hints in all languages. But I am not sure, if it is a good idea.
Thanks in advance.
A common way that is used is to have an XML-file for each language. The XML-file should contain every phrase you use in your game with a unique ID.
In your code, you can then get the correct phrase out of the current language's XML, using the correct ID.
If you don't know about XML files, read this tutorial about using XML files in Unity: http://xeophin.net/en/blog/2010/05/13/reading-strings-out-xml-file-using-c-unity-3d
However if you are willing to spend some money on your game, you should check this out: http://u3d.as/content/rodrigo-barros/my-menu/32E
I haven't tried that yet but it seems good.
Hardcoding is not that bad if you do it correctly.
For example create an abstract class with all the messages that you want to have and language autodetection:
public abstract class Lang {
static Lang currentLang;
public static Lang Get {
get {
if(currentLang == null)
switch(Application.systemLanguage) {
case SystemLanguage.Polish:
currentLang=new LangPL();
break;
default:
case SystemLanguage.English:
currentLang=new LangEN();
break;
}
return currentLang;
}
}
public abstract string MenuTime {get;}
public abstract string MenuPoints {get;}
public abstract string YouWon {get;}
public abstract string YouLost {get;}
public abstract string Point(int p);
}
And then implement each language as a separate class:
public class LangPL:Lang {
public override string MenuTime {get {return "CZAS";}}
public override string MenuPoints {get {return "WYNIK";}}
public override string YouWon {get {return "Gratuluję, wygrałeś!";}}
public override string YouLost{get {return "Może następnym razem...";}}
public override string Point(int p) {
if(p == 1)
return "1 punkt";
return p+" punktów";
}
}
public class LangEN:Lang {
public override string MenuTime {get {return "TIME";}}
public override string MenuPoints {get {return "SCORE";}}
public override string YouWon {get {return "You won!";}}
public override string YouLost{get {return "Maybe next time...";}}
public override string Point(int p) {
if(p == 1)
return "1 point";
return p+" points";
}
}
Usage is a simple:
GUILayout.Label(Lang.Get.YouWon);
GUILayout.Label(Lang.Get.Point(5));
This way you have most control, and can easily support complex translations. Also this approach helps to trim down on errors - if you forget or misstype a text anywhere it throws a syntax error.
You can try to create a Javascript script or a C# script for each language and then depending of the language choosen, you call the specific script.
e.g
public class English : MonoBehaviour{
public string jump = "Press 'space' to jump";
//etc
}
public class French : MonoBehaviour{
public string jump = "Appuyer sur 'espace' pour sauter";
//etc
}
In your function for GUIText
public GameController gameController;
public GUIText text;
private string languageSelected;// obtain from the user input on language selection (better if registered in the gameController)
void Awake(){
languageSelected = System.Activator.CreateInstance(Type.GetType(gameController.languageSelected));
// where languageSelected will be e.g English
text.text = languageSelected.jump;
}
I strongly advise against embedding text into your code. It will slow down the localization process (having to look through code to find text is not the quickest and best way to approach localization).
Go with separate files. It's easier to maintain and translators don't have to access the code.
Have a look at l2 localization. It's a pretty handy Unity plugin that allows you to manage translations of your game.
All your localized strings in a single Google Spreadsheet. Once set up, all you need to do is fill it out to see the results in the game.
I am a game translator myself and happen to be managing a small team of game translators called Level Up Translation. We posted a beginner's guide for localization in Unity in our blog a couple of months ago. Feel free to check it out!
For example, you can save language file for france.txt in format:
hello=Bonjour
goodbye=Au Revoir
And than, read file and convert it to dictionary:
string[] lines = File.ReadAllLines("france.txt");
var dict = lines.Select(l => l.Split('=')).ToDictionary(a => a[0], a => a[1]);
Now you can read from dictionary by key:
string hello = dict["hello"];

how to convert Enum to IEnumerable<SelectListItem>

UPDATE 1
I want to populate a listboxfor mvc helper to render list items in View. I don't know how to pass the second parameter to the helper, which accepts IENumerableSelectListItem SelectionList.
Html.ListBoxFor(model => model.SelectedDate, Model.DateList, new { })
I am having a property in my model class,
public IEnumerableSelectListItem DateList { get; set; }
and somehow i need to populate DateList from my controller action method, which fills up all the other model properties and passes it to a partial view where i have the helper. DateList should be getting the values from an Enum DateListEnum
Any ideas on how to go about doing this. Also let me know how do i ensure that a single list item is selected at a time...i am really new to all these..and its been quite some time i am working on this...but nothing came up so far....
I am having below code in my controller action method,
model.DateList = getDateList();
which calls this method,
public IEnumerableSelectListItem getDateList()
{
IEnumerableSelectListItem values = Enum.GetValues(typeof(DateListEnum));
//this one doesn't work because of casting issues and this is where i want to convert Enum Values to IEnumerableSelectListItem
return values;
}
UPDATE 2
i have the listbox working and its displaying in the UI based upon below code
` IEnumerable<SelectListItem> values = Enum.GetNames(typeof(ColumnFormattingDateFormats)).Cast<ColumnFormattingDateFormats>().Select(p => new SelectListItem()
{
Text = p.ToString(),
Value = ((int)p).ToString()
}).ToList();`
But now i am having few other problems,
the enum that i am using is
`public enum ColumnFormattingDateFormats : int
{
[StringValue("Wednesday, March 14, 2001")]
FULLDATE = 0,
[StringValue("3/14")]
MMDDWSLASH = 1,
[StringValue("3/14/01")]
MMDDYYWSLASH = 2,
[StringValue("03/14/01")]
MMDDYYWMMZEROPREFIXWSLASH = 3
}`
and my helper looks like this,
Html.ListBoxFor(model => model.SelectedDate, Model.DateList, new { })
1> How do i pass the selected item to the listboxfor helper?
is there a way to pass the selected item to the helper through the property
[DataMember]
public ColumnFormattingDateFormats SelectedDate{ get; set; }
in my model class? to begin with i am passing
this.SelectedDate= ColumnFormattingDateFormats.FULLDATE;
to the default constructor in my model class...but for some reason the first parameter model => model.SelectedDatehas some problem with that..throws null exception...
2> how do i ensure [StringValue] from Enum gets displayed in UI listbox element and not the Enum Text for ex. FULLDATE should not be getting displayed, instead "Wednesday, March 14, 2001" should be?
3> How do i ensure that a single item is selected without using livequery?
My brains are all fried up now....any directions anyone?????
how about:
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Select(p => new SelectListItem()
{
Text = p.ToString(),
Value = ((int) p).ToString()
})
.ToList();

Bind /action/1,2,3 to List<int>

In my API, I'd like to have routes like GET /api/v1/widgets/1,2,3 and GET /api/v1/widgets/best-widget,major-widget,bob-the-widget
public class WidgetsController : MyApiController
{
public ActionResult Show(IEnumerable<int> ids)
{
}
public ActionResult Show(IEnumerable<string> names)
{
}
}
I've got routes set up to get me to the action, but I can't figure out how to turn 1,2,3 into new List<int>(){1, 2, 3} and so on. Of course, I could just take a string and parse it in my action, but I'd like to avoid going that route.
One thing that came to mind was to put something in the OnActionExecuting method, but then I wasn't sure exactly what to put in there (I could hack something together, obviously, but I'm trying to write something reusable.)
The main questions I have are how to know whether I need to do anything at all (sometimes the ValueProviders upstream will have figured everything out), and how to handle figuring out the type to cast to (e.g., how do I know that in this case I need to go to a collection of ints, or a collection of strings, and then how do I do that?)
By the way, I had the idea of implementing a ValueProvider as well, but got lost on similar questions.
I can't figure out how to turn 1,2,3 into new List(){1, 2, 3} and so on.
To avoid polluting each controller action that needs to receive this parameter I would recommend a custom model binder:
public class IdsModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var result = base.BindModel(controllerContext, bindingContext);
var ids = bindingContext.ValueProvider.GetValue("ids");
if (ids != null)
{
return ids.AttemptedValue
.Split(',')
.Select(id => int.Parse(id))
.ToList();
}
return result;
}
}
and then register the model binder in Application_Start:
ModelBinders.Binders.Add(typeof(IEnumerable<int>), new IdsModelBinder());
and finally your controller action might look like this (and from what I can see in your question it already does look like this :-)):
public ActionResult Show(IEnumerable<int> ids)
{
...
}
and the custom model binder will take care for parsing the ids route token to the corresponding IEnumerable<int> value.
You could do the same with the IEnumerable<string> where you would simply remove the .Select clause in the corresponding model binder.
if your URL was
/api/v1/widgets/Show?names=best-widget&names=major-widget&names=bob-the-widget
This would bind neatly by itself :)
No need to override modelbinders in this case.
The querystring-variable names will bind to your Show-method_
public ActionResult Show(IEnumerable<string> names)
Hope this helps!
I'm relatively new to ASP.Net MVC and so I'm not sure if there is an easier way of doing this or not, however my approach would be to do something like the following:
public class WidgetsController : MyApiController
{
public ActionResult Show(string ids)
{
List<int> parsedIds = new List<int>();
foreach (var id in ids.Split(','))
{
parsedIds.Add(int.Parse(id));
}
return Show(parsedIds);
}
private ActionResult Show(List<int> ids);
}
You might also want to add some more sophisticated error handling for cases where the IDs entered can't be parsed, but thats the general approach I would use.