Puting a BoxSizer in the middle of its panel - user-interface

I've written some code which displays a wx.Frame, within which there is a wx.Panel, within which there is a jpg. The jpg is the same size as the frame, so that when the program starts up the user is presented with a picture covering the whole of the window. Overlayed over the top of this picture, and in the centre of the panel I want to put a wx.BoxSizer containing various widgets. So far I've got the sizer visible but, try as I might, I cannot shift it from the uppermost lefthand corner of the panel to the center. No doubt there are many round-about ways of doing this involving grids and borders, but before I set off down this path, I was wondering if I'm missing something simple and there is a command that can be used to position in widget in a specified part of a panel.
ps. I also know I could do this using absolute positioning, but I want to be able to resize the window while keeping the sizer at its centre.

Normally, you can't see a sizer. But regardless, you don't really center a sizer. You center the widgets INSIDE the sizer. So when you add a widget to the sizer, you'd want to do something like this:
mySizer = wx.BoxSizer(wx.VERTICAL)
mySizer.Add(widget, 0, wx.CENTER|wx.ALL, 5)
Now, that will only center it horizontally. If you want it in the very center of the panel, then create another BoxSizer that is oriented horizontally and add the first sizer to the second one, centering it too.
hSizer = wx.BoxSizer(wx.HORIZONTAL)
hSizer.Add(mySizer, 0, wx.CENTER)

Related

Reset (or change Pivot of) an Image in ScrollRect when scaling down

I have a single image that is displayed inside a scrollRect.. pivot is centered and the user is able to scale the image up and down using a slider.. then he/she is able to scroll drag the image around (with the scrollRect functionality) like for example a page in a book-app.. but when the user scales the image up and drags it around and then scales it back down again, the page shrinks at its own pivot point and sometimes gaps are visible on the sides depending on where the user has dragged the image.. When you start to drag the image again it snaps to the correct location but it doesn't look very polished when you force a user to "nudge" something before it snaps into place..
does anybody know of a way to invoke the "nudge" after scaling? Or force the ScrollRect to calculate the position of the image after scaling? I tried using math and pivot points but since the position alters with both scaling and dragging I can't hardcode a value to change pivot points (like f.e. if x < -50 and y < -50 then the pivot point should be top right) ..
The simplest solution would be to invoke that little nudge the user has to do now but I don't know how I have to go about doing it (just changing the transform.positon.x in code doesn't work, the scrollRect needs to update its content) or maybe I could reset the position of the anchor so it stays at the center of the parent..
I tried to clarify with some images:
https://ibb.co/f7Oxk6

background image not working right

my goal: have a background image which occupies the full window of the display. And then on it, place image buttons which will be nicely centered on this window (buttons being arranged in grid).
The image is 1920X1080.
I have other code snippet which follows exactly the same sequence and the image appears in background. So totally baffled. But wanted to understand the logic correctly of this code/instead of relying on luck!..Note that on the other working code, I do both a place and grid of the background_label !. With this code the buttons don't appear at all.
when I use background_label(content....), the image does appear in the background with buttons in front, but in that case the whole display window is not occupied. The image just expands to fill the area occupied by button images.
root = Tk()
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
content = ttk.Frame(root, padding=(3,3,12,12))
print(root.winfo_screenwidth(), root.winfo_screenheight())
#prints as 1920 & 1080
#old background_image_file='waves1600x926.gif'
background_image_file='Lake.gif' #this is 1080X1920
background_image=PhotoImage(file=background_image_file)
background_label =ttk.Label(root,image=background_image) #tried content instead of root also
background_label.place(x=0, y=0,relwidth=1, relheight=1)
background_label.grid()
logos=['abc','cbs','nbc','fox','cnbc','amc','bet']
logobuttons=defaultdict(str)
logoimgs=defaultdict(str)
for logo in logos:
logoimgs[logo]=PhotoImage(file=LOGODIR+logo+'.gif')
logobuttons[logo]=ttk.Button(content,image=logoimgs[logo])
content.grid(column=0, row=0, sticky=(N,S,E,W))
col=0
row=0
maxcols=5
for logo in logos:
logobuttons[logo].grid(row=row,column=col,pady=5,padx=5)
if col == maxcols:
col=0
row=row+1
else:
col=col+1
root.mainloop()
When you use both place and grid, only the last one you call for a given widget has any effect. Thus, when you call background_label.grid() it totally negates the effect of background_label.place(...). In other words, you need to remove the call to grid for this widget.
If you want a background image, the best solution is to use place with a relative width and height of 1. You should also create the widget that contains the image first, so it is lowest in the stacking order. Though, you can always lower it later.

Unity 4.6 - How to scale GUI elements to the right size for every resolution

The new Unity 4.6 comes with a new GUI, when I change de resolution on Unity the UI Button scales perfectly but when I test on the Nexus 7 device the Button looks too small. Any idea how to solve this?
Unity's new GUI system uses "anchors" to control how gui elements (like buttons) scale in relation to their parent container.
Unity has a tutorial video on how to use the new "Rect Transform" component (where the anchors are configured) here: http://unity3d.com/learn/tutorials/modules/beginner/ui/rect-transform.
The last half of the tutorial is all about anchors. That page has links to the entire tutorial series. It's not too long. You should watch the whole thing.
Specific to your question:
The anchors are visible in your first screen shot. They are those 4 little arrows at the top left of your button.
Right now, your button is only anchored by it's top left corner.
The two right anchors need to be dragged to the right so that the right edge of your button is anchored to a space inside its parent container.
Depending on your situation, the two bottom arrows may need to be dragged down so that the bottom edge of your button is anchored as well.
The video I linked above covers all this in detail.
Lastly, for the font size to scale nicely on different resolutions, you will need to add and configure a reference resolution component to the base canvas of your UI, as Ash-Bash32 wrote earlier.
Update: The best way to add a Reference Resolution component is through the inspector window for the base canvas in your UI.
1) click the "Add Component Button" at the bottom of the inspector.
2) type the word "Reference" in the search filter field.
3) select the "Reference Resolution" component in the search results.
The Reference Resolution is now renamed as Canvas Scaler.. Along with the renaming they have added many more features for the dynamicity of the Canvas. You can go through the Unity Doc of Canvas Scaler and also take a look at this article for a practical example of how and why to use Canvas Scaler. Also make sure you use the Anchor Points to good effect to make this more robust...
To Scale UI added the ReferenceResolution Component to the Canvas you want to scale.
P.S. Theres no Documention for ReferenceResolution
If you want the button to be the same size for all screens and resolutions, you have to add the canvas scaler component to the canvas and the set the screen match mode to: match width or height, here is the link to the docs, this helps a lot if you want to aim to different sizes or resolutions:
http://docs.unity3d.com/Manual/HOWTO-UIMultiResolution.html
This becomes giant and convoluted once you start laying things out in code AND using a canvas scaler, so I wish to provide a thorough answer to save someone the hours I went through.
First, don't use anchoredPosition to position anything, unless you fully realize it is a 0.0 to 1.0 number. Use the RectTransform localPosition to do the actual laying out, and remember it's in relation to the parent anchor. (I had to lay out a grid from the center)
Second, put a canvas scaler on the parent layout object AND the inner ui pieces. One makes the layout in the right position, the other will resize your elements so they actually show up right. You can't rely on the the parent unless the children also have scalers (and Graphic Raycasters to touch them).
Third, if you have a scaler, DON'T use Screen.width and height, instead assume the screen is the same value you put for the scalers (hopefully you used the same, or know what you're doing). The screen width always returns the actual device pixels, retina devices too, but the canvas scalers DO NOT account for this. This probably gives unity the one remaining way to find actual screen dpi if your game wants it. Edit: This paragraph applies to any parent canvas connected to the code doing your laying out. Not stray canvases, you can probably mix it up. Just remember unity's guidelines on performance with canvases.
Fourth, the canvas is still a bit buggy. Even with the above working, some things don't render until you delete and recreate a canvas, if you re-open the scene or it crashes. Otherwise, the above is the general "rules" I've found.
To center a "grid of things" you can't just use half of the canvas scaler's width or height, you have to calculate the height of your grid and set the offset by half of it, otherwise it will always be slightly off. I just added this as an extra tip. This calculation works for all orientations.

Cocoa: Custom control not limited to window frame - how to start?

I want to build a custom control that would work like this:
You have a kind of NSButton with an image.
You click the button and than appears a big square with a grid of photos.
You click one of the photos and it is set up as new image for the button. (square dissapears)
Now, how to draw this big square with photos if I want it not to be limited to window frame?
I mean, if the button was close to window border the square is going to be partially outside window. I would also like to add some shadow to the square and an animation for opening/closing.
One important thing: I want to be able to draw not only a square but any other simple shape (circle)!
This isn't really a drawing question so much as a general custom views question. It's important to make that distinction.
I'll describe this in terms of rectangles to give you the general idea*. You should make sure you understand the view hierarchy and view geometry in Cocoa. Without this important requisite knowledge, you'll remain dead in the water.
It's easy to set an NSButton's image, so I'll leave that to you. Your button's action, however, would tell some controller to show the "image picker" for the given button. Your image picker would be some type of borderless window with an image list inside. The image picker could be an IKImageBrowserView (you'll have to enable Image Kit in Interface Builder for this control to appear), which gives you an iPhoto-like grid of images (with/without titles, different border types, etc.).
An explanation of the operation of this controller and how it creates the window, manages the selection, and sets the button's image is very broad so if you get hung up on any of those steps, you'll need to create a separate question for each problem, otherwise this answer would have to be an instruction manual for writing your app for you.
* Your problem is a little more difficult because of your desire to have differently-shaped "popup windows" ... you'd have to make sure your available photos fit neatly within the shape so none of them are cut off. Armed with the basic knowledge of view geometry, I'll leave this to you as an exercise. A hint: you can use a borderless, transparent window to host a view that draws itself in any shape you please.

Zooming an image inside a picture box

I'm have a picture box control and 2 Command Buttons. I have an image displayed inside the picture box.
Is it possible to zoom the image when the Zoom-in and Zoom out buttons are clicked?
Or I can even put a scroll bar. Is it possible to zoom the image according to the scroll bar movements?
I'm using VB 6.
I assume here that you are using BMP or JPG files here.
The simple scratch method is to place an Image control in the PictureBox, initially with the property Stretch = False. Initially, it would be in the top left hand corner. After setting the Picture property to your picture object, the Image control will be resized to fit the image. Save the original width and height of the control in variables. Now set Stretch = True. You can zoom in by resizing the image using
img.Move 0, 0, sngWidth * sngMagFactor, sngHeight * sngMagFactor
Where sngMaxFactor = 4! or however much you want to zoom by.
Restore back to original size by:
img.Move 0, 0, sngWidth, sngHeight
You can also pan the zoomed image by altering the Left and Top arguments in the Move() method.
It might be easiest to use two pic boxes, one inside the other. The 'outer' box can be thought of as a viewport into the 'inner' box, which you resize and position as needed. The effect will be the same but the coding is much simpler.

Resources