Slider: How to snap to one tick, but not others - wpf

I'm making a media player speed selector, which goes from 0.5 to 2. This is how I have defined the Slider.
<Slider Maximum="2" Minimum="0.5" Ticks="0.5 0.75 1 2"/>
This is because 0.5, 0.75, 1 and 2 is the speeds that is the most desired play-speeds. So I could, if that was the only requirement, say IsSnapToTicksEnabled="True", but the user should also be allowed to select an value between these ticks, and at the same time still be able to snap to the specified ticks.
So, the thumb should snap to these ticks once the slider is close, but not when further away than ~0.1. Is this possible?
We also have the RadControls from Telerik, if there is something specifically implemented there.
What I've tried so far is to listen to ValueChanged and check if the new value is between 1.1 and 0.9, and set it to 1 if it is. But that disables dragging completely.

I did something similar.
I created 2 sliders:
One of them is invisible (from 0 to 100 for example), it's just a rectangle (invisible color) on top on the other slider, this is the one you will slide with your mouse.
The second slider is your graphic, from 0.5 to 2, like you actually have.
Now you just have to code your invisible slider, for example:
If InvisibleSlider < 10, then RealSlider stay at 0.5
If InvisibleSlider is between 10 and 30, then the value of RealSlider is InvisibleSlider * X ?
With that you can simulate that you stick where you have decided, eg: stick at 0.75 if the invisible slider is between 40 and 60, your mouse will move but the the graphic...
I don't know if I'm clear, and english is not my main language sorry

Related

Leaflet: How to control opacity of Layer Groups?

Does some expert know how to change the opacity of each tilemap of a Layer Group by using a HTML-slider input?
For example: I've got several tilemaps, which could be switched by using Lealet's Layercontrol button, like here: Leaflet Layer Groups.
When using the opacity-slider I want to dim each tilemap, not just one being actually active. For example: I'm dimming map1 to 0.5
Then switching to map2, its opacity should already also be 0.5. And when changing the opacity of map2 to 0.8 with the slider and switching back to map1, map1 should already have opacity 0.8 again (not the former adjusted value of 0.5)
I know how to implement the slider control and how to change the opacity of a single tilemap by using the command
nameOfMaplayer.setOpacity(opacityValue);
But I don't have an idea how to reference using Leaflet's methods to the Pane/Grid Layer/array of all tilemaps to change the map's pane opacity instead of the opacity of each indivdual tilemaps simultaniously.
I think in the end I found a satisfying answer myself:
If we want to change the opacity of the tilePane (= basemap-layers AND overlaymap-layers):
map.getPane('tilePane').style.opacity = 0.5;
But this has the disadvantage that also the Overlay-tilemaps get dimmed which I wanted to avoid.
My prefered method is to change just the opacity of the active basemap-layer. Since Leaflet doesn't support getting the active basemap Layer, you'll have to use the "Activelayers"-Plugin and use its methods:
ctrLayer = L.control.activeLayers(baseMaps, overlayMaps, {position: 'topright'}).addTo(map);
.....
tilemapLayer = ctrLayer.getActiveBaseLayer().layer;
tilemapLayer.setOpacity(actualOpacityValue);
Each time you change the basemap, you have to run the later 2 commands to change the new basemap's oppacity to the actual used opacity value.

How a Slider control in WPF can snap on specific values?

I want to create a slider, with values from 0 to 100 that I can slide like any other slider... but at position 30, 42 and 55 (for example) I want to snap to these values, to make easy to the user to stop the slider at them
edit: my solution was to have 2 slider, the first one is invisible, value 0 to 200, and the other one is the visible on, value 0 to 100
The visible one cannot be slide, only the the invisible. Like this i can make a gap, ex: when I'm between 50 and 75 on the invisible slider, it's equals to 50 on the visible one...
You have to override OnValueChanged. See this article DiscreteSlider - Adding Functionality with a Simple Control Subclass and then this artice Silverlight slider control that snaps for a detailed explanation.
Instead of using SmallChange, you would check where between your values the slider sits and snap to the nearest.

calculate selection brush color in WPF

I have noticed when setting the selection of a text box in wpf to red, and using a color picker to verify the color, the color faded to #FF9999.
I have a specific color my client requires for the selection brush. Is there a way to calculate what color i should set the SelectionBrush to so when the text is selected, the exact color is displayed?
Currently I need to have the selection brush as #6599D1, but once set, the color is #C1D6ED.
How can I calculate the starting color so the end color is what I need?
Following up on the answer by H.B.
I've calculated opacity with the following formula in the past
Red = OriginalRed * Opacity + (1-Opacity) * BackgroundRed
which inverts to
(Red - (1-Opacity) * BackgroundRed) / Opacity = OriginalRed
The TextBox has default Background set to White and SelectionOpacity set to 0.4.
As H.B. explained, you can't achieve your color with these default settings since the Red value will come out as -130. This leaves you with 3 options, Change Background, change SelectionOpacity or don't do it :)
Changing the TextBox Background probably isn't something you wanna do so that leaves option 2, change SelectionOpacity
We don't want Red to go below 0 so
(101 - (1-Opacity) * 255) = 0
which gives
1 - (101/255) = Opacity
This results in 0,604 so with this SelectionOpacity value you can calculate that the SelectionBrush needs to be set to #0056B3 to become #6599D1 once the Opacity has been applied.
Here's how the TextBox look with these values
<TextBox SelectionBrush="#0056B3"
SelectionOpacity="0.604" />
Good question but i think this is impossible. If there is some overlay inside the ControlTemplate you cannot formulate a function which calculates a darker colour which then will end up as the intended colour.
e.g. when you input red which is: 255,0,0 you get 255,153,153, now the function that would need to be applied to your initial colour would need to darken the Red, this of course could only be done in the red channel as green and blue are already zero. The problem is not the red channel however (which ends up as 255 and hence is not affected), so any changes to that will only serve to desaturate the colour even more instead of darkening it.
Edit: To make this a bit more mathmatical, the function that is applied by the transparency of the selection is:
f(x) = 0.4x + 153
If you apply this to all the channels of your colour you will see that this is indeed the case. Now how do we get the values we want? Quite simple, we invert the function, which is this:
f^(-1)(x) = -2.5(153.0 - x)
Great! Now lets apply that to your colour:
R: -130.0
G: 0
B: 140
Hmm, not so great after all i suppose.
This negative value is exactly why this is not always possible, every colour which has components below 153 is not reversible.

Silverlight Planeprojection

I have a user control that has a white background with a Zindex = 0. I also have an image on top of the white background with a Zindex = 10. When I use planeprojection to flip the image 180 degrees on the X-axis it just shows my image in reverse. How would I get it to show the white background when it gets flipped?
PlaneProjection inherently won't show the "back side" of a container. What you need to do:
Define two elements in your container: one for the front/visible side and one for the back/hidden side
When your PlaneProjection rotation is greater than 90 degrees, hide the front element, and show the back element. Vice-versa for switching back to < 90 degrees
I believe you can use the VisualStateManager to intercept when the rotation angle hits a certain value and to then apply the visibility.
Here is a Tim Heurer article on the VSM:
http://timheuer.com/blog/archive/2008/06/04/silverlight-introduces-visual-state-manager-vsm.aspx

Silverlight ProjectionPlane Oddity

I have this panel in my app which is 10400 pixels wide.
I have my CenterOfRotationX and CenterOfRotationZ = 0.5.
I have the GlobalOffsets configured so that the rotation of the panel is visible on the screen.
This video shows the RotationY being set from -180 to 180.
http://www.youtube.com/watch?v=zDrETOueb-w
Its really weird at RotationY = 90 (about 13 seconds on the video), it seems to get stretched to hell when I would expect it to disappear from view.
Also from about 8 - 9 seconds on the video shows the panel starting at RotationY = 0 to RotationY = 20, where it starts to stretch. Over this small rotation it appears to nearly rotate 180 degrees.
Maybe I have some settings wrong but this seems really strange. - The only value changing in this video is the RotationY.
The problem was with the GlobalOffsetX variable on the ProjectionPlane.
This was set to something astronomical so I could see the full rotation on the screen but this had an effect on the rotation.
Setting this to 0 and then moving the Plane to the left using the Canvas.LeftProperty fixed this.

Resources