I am writing an app where I will give a set of tests/checks to the user through an UI such as:
LayerCount
ActiveLayerType
EffectCount
ActiveEffectType
CurrentTool
HasSelection
HasAlpha
etc
where the user is able to use them to define a custom logic tree such as:
if (LayerCount > 1)
{
if (ActiveLayerType == LayerType.Blend)
{
// #1
}
else if (ActiveEffectType == EffectType.GaussianBlur)
{
// #2
}
}
else if (CurrentTool == Tools.QuickSelect)
{
if (HasSelection)
{
// #3
}
}
Basically I am trying to define some sort of value that will return the Current Level
in his custom logic in some way (let's say 1, 2, 3
, and so on). Because I need to have the actions of the user interpreted differently based on the Current Level
in his logic.
You could basically say that I am trying to hash a set of values based on the Current Level
. But the problem is, I don't know how to get the current level to store it as a hash key. If I did, I could write something like:
var currentActionSet = ActionSetTable [GetCurrentLevel()]
and I would get a set of actions the user can perform based on the current level.
Although it would be more desirable to have these levels have fixed values instead of values starting from 1 to n, based on how they are ordered. So (ActiveEffectType == EffectType.GaussianBlur)
's branch would always return a fixed value no matter where it's in the logic 开发者_StackOverflow中文版tree.
So to sum up, the problems are:
- How to define a current level value, preferrably fixed?
- If using this current level value as key is the best approach, or if there is a better way of doing this?
Perhaps you can use bitflags and OR them together to make your hashcode?
int hash = ActiveLayerType | ActiveEffectType | CurrentTool
Then at some later point you can AND a perticular flag out again:
if( hash & LayerType.Blend == LayerType.Blend ) { ... } //will be true if the LayerType.Blend has previously been OR:ed into the hash variable
You can OR together enums of diffrent types too if you cast them to ints first, make sure that the enum bit values doesnt clash though.
This approach may or may not be practical for you application, but its something to consider :) more info here: http://msdn.microsoft.com/en-us/library/cc138362.aspx
精彩评论