Unity3D ScrollView scroll range - layout

I'm trying to create a scrollView, but I don't know how to determine the maximum range of the scroll (or even better, the maximum scroll position).
This is what I have tried to do without any positive results:
void Update()
{
//get the amount of space that my text is taking
textSize = gs.CalcHeight(new GUIContent(text), (screenWidth/2));
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(0).tapCount==1)
{
var touch = Input.touches[0];
//check if touch is within the scrollView area
if(touch.position.x >= (screenWidth/2) && touch.position.x <= (screenWidth) && touch.position.y >= 0 && touch.position.y <= (screenHeight))
{
if(touch.phase == TouchPhase.Moved)
{
scrollPosition[1] += touch.deltaPosition.y;
if(scrollPosition[1] < 0)
{
scrollPosition[1] = 0;
}
//I subtracted this cause i read that the scrollbars take 16px
if(scrollPosition[1] > (textSize-scrollViewRect.yMax-16)) //scrollViewRect is a Rect with the size of my scrollView
{
scrollPosition[1] = textSize-scrollViewRect.yMax-16;
}
}
}
}
}
void OnGUI()
{
screenWidth = camera.pixelWidth;
screenHeight = camera.pixelHeight;
GUILayout.BeginArea(new Rect(screenWidth/2, 0, screenWidth/2, screenHeight));
GUILayout.BeginScrollView(scrollPosition/*, GUILayout.Width (screenWidth/2), GUILayout.Height (screenHeight/2)*/, "box");
GUILayout.Label (new GUIContent(text), gs);
// End the scrollview
GUILayout.EndScrollView ();
scrollViewRect = GUILayoutUtility.GetLastRect();
GUILayout.EndArea();
}
This was done assuming that the scrollView is on the right half of the screen.
Can you guys help me out on this?
Thanks in advance.

As shown in the API documentation for GUI.BeginScrollView, you can assign a Vector2 to the declaration of BeginScrollView to track and update where the box has been scrolled to.
I'll give an example:
using UnityEngine;
using System.Collections;
public class ScrollSize : MonoBehaviour {
private int screenBoxX = Screen.width;
private int screenBoxY = Screen.height;
private int scrollBoxX = Screen.width * 2;
private int scrollBoxY = Screen.height * 3;
public Vector2 scrollPosition = Vector2.zero;
void Awake()
{
Debug.Log ("box size is " + scrollBoxX + " " + scrollBoxY);
}
void OnGUI()
{
scrollPosition = GUI.BeginScrollView(new Rect(0, 0, screenBoxX, screenBoxY),
scrollPosition, new Rect(0, 0, scrollBoxX, scrollBoxY));
GUI.EndScrollView();
// showing 4 decimal places
Debug.Log ("Box scrolled # " + scrollPosition.ToString("F4"));
}
}
For the explanation of this example, let's assume that the screen resolution is 800 by 600.
Therefore the viewable scrollbox on the screen will be 800 wide and 600 high, with the internal area of the scrollbox being 1600 by 1800.
We create the scrollbox by assigning Gui.BeginScrollView() as the value of Vector2, scrollPosition. As the scrollbox is scrolled horizontally and vertically, the Vector2 will update with the scrolled value.
If the scrollbox is scrolled to the top-leftmost position, the value of scrollPosition will be 0,0.
If the scrollbox is scrolled to the bottom-rightmost position, the value of scrollPosition will be 816, 616, the size of the box on screen plus the thickness of the scroll bars.

I know this is 3 years later but maybe it will help others too...
I found that setting the scrollPosition to a very high number will force the scrollbar to the bottom:
scrollPosition.y = 999.9f;
Then you can read the position like this:
scrollPosition = GUILayout.BeginScrollView(scrollPosition,
GUILayout.Width(370),
GUILayout.Height(135));
And it will be set to whatever the maximum is.
Hope this helps!

Related

How to resize a SVG element composed of multiple paths in JavaFX?

I'm remaking the design of an old javafx app and I need to include an icon for the wifi strength. The designer sent me an svg icon for that, and I thought it was smarter to just keep one icon and use different fill colors for the 3 bars depending of the said wifi strength.
I found a nice approach for that, and it works quite well until you need to resize the wifi icon. It seems like even if I change the -fx-pref-width (or height, or min/max) the svg icon keeps its size.
I also tried to resize each svg path one by one but it only goes messy with the spaces between them. And using a unique shape to resize later is not an option, as I need at least 2 colors. FYI the goal is to apply a 5em or 3em size depending of the context.
Here's the code I currently have, where everything looks great if you don't matter the icon size :
public class WifiStrengthRegion extends Pane {
public WifiStrengthRegion() {
getStyleClass().setAll("wifi-strength");
SVGPath round = new SVGPath();
SVGPath bar1 = new SVGPath();
SVGPath bar2 = new SVGPath();
SVGPath bar3 = new SVGPath();
round.setContent("M253.5,336.5c-18.9,0-34.2,15.3-34.2,34.1c0,18.8,15.4,34.1,34.2,34.1c18.9,0,34.2-15.3,34.2-34.1 C287.7,351.8,272.4,336.5,253.5,336.5z");
bar1.setContent("M337,290.1c-22.3-22.3-51.9-34.5-83.5-34.5c-31.4,0-61,12.2-83.3,34.3c-9,9-9,23.5,0,32.5 c4.4,4.4,10.2,6.8,16.3,6.8c6.2,0,11.9-2.4,16.3-6.7c13.6-13.5,31.6-20.9,50.7-20.9c19.2,0,37.3,7.5,50.8,21 c4.4,4.4,10.2,6.8,16.3,6.8c6.2,0,11.9-2.4,16.3-6.7C346,313.6,346,299,337,290.1z");
bar2.setContent("M389.3,238c-36.3-36.2-84.5-56.1-135.8-56.1c-51.2,0-99.3,19.9-135.6,55.9c-4.4,4.3-6.8,10.1-6.8,16.3 c0,6.1,2.4,11.9,6.7,16.3c4.4,4.3,10.2,6.7,16.3,6.7c6.2,0,11.9-2.4,16.3-6.7c27.5-27.4,64.1-42.5,103-42.5 c39,0,75.6,15.1,103.1,42.6c4.4,4.4,10.2,6.8,16.3,6.8c6.2,0,12-2.4,16.3-6.7c4.4-4.3,6.8-10.1,6.8-16.3 C396,248.1,393.6,242.3,389.3,238z");
bar3.setContent("M444.3,183.2c-50.9-50.8-118.7-78.8-190.8-78.8c-72,0-139.7,27.9-190.6,78.6c-9,9-9,23.5,0,32.5 c4.4,4.3,10.2,6.7,16.3,6.7c6.2,0,12-2.4,16.3-6.7c42.2-42,98.3-65.2,158-65.2c59.7,0,115.9,23.2,158.1,65.3 c4.4,4.3,10.2,6.7,16.3,6.7c6.2,0,11.9-2.4,16.3-6.7C453.2,206.7,453.3,192.1,444.3,183.2z");
round.getStyleClass().add("wifi-base");
bar1.getStyleClass().add("wifi-bar1");
bar2.getStyleClass().add("wifi-bar2");
bar3.getStyleClass().add("wifi-bar3");
this.getChildren().addAll(round, bar1, bar2, bar3);
}
public void setWifiStrength(Integer strength) {
if (strength == null) {
setManaged(false);
setVisible(false);
} else {
setManaged(true);
setVisible(true);
getStyleClass().removeAll("wifi-excellent", "wifi-good", "wifi-fair", "wifi-weak", "wifi-off");
if (strength < 0 && strength >= -100) {
if (strength >= -50) {
getStyleClass().add("wifi-excellent");
} else if (strength >= -70) {
getStyleClass().add("wifi-good");
} else if (strength >= -80) {
getStyleClass().add("wifi-fair");
} else {
getStyleClass().add("wifi-weak");
}
} else {
getStyleClass().add("wifi-off");
}
}
}
}
(and then a css stylesheet applies -fx-fill to each .wifi-barX depending of the main element class)
And here is an example of how the svg icon looks like:
I'm a very beginner in Java (and obv JavaFX), so any constructive criticism will be appreciated!
If you want to resize only (I hope this works):
public void resize(SVGPath svg, double width, double height) {
this.width = width;
this.height = height;
double originalWidthR = svg.prefWidth(-1);
double originalHeightR = svg.prefHeight(originalWidthR);
double scaleXr = width / originalWidthR;
double scaleYr = height / originalHeightR;
svg.setScaleX(scaleXr);
svg.setScaleY(scaleYr);
}
And if you want to set bounds use this:
public void setBounds(SVGPath svg, double width, double height, double x, double y) {
this.width = width;
this.height = height;
double originalWidthR = svg.prefWidth(-1);
double originalHeightR = svg.prefHeight(originalWidthR);
double scaleXr = width / originalWidthR;
double scaleYr = height / originalHeightR;
svg.setScaleX(scaleXr);
svg.setScaleY(scaleYr);
svg.setLayoutX((originalWidthR - width) + x);
svg.setLayoutY((originalHeightR - height) + y);
}

is there a way to adjust combo list width dynamically in vc++

I'm trying to adjust combo list width based on list content maximum width.
what are all the possible ways to increase/decrease width of combo list dynamically depends upon on the list items maximum width.
I have used this:
void SetBestDroppedWidth(CComboBox *pComboBox)
{
// Find the longest string in the combo box.
CString str;
CSize sz;
int dx = 0;
TEXTMETRIC tm;
CDC* pDC = pComboBox->GetDC();
CFont* pFont = pComboBox->GetFont();
// Select the listbox font, save the old font
CFont* pOldFont = pDC->SelectObject(pFont);
// Get the text metrics for avg char width
pDC->GetTextMetrics(&tm);
for (int i = 0; i < pComboBox->GetCount(); i++)
{
pComboBox->GetLBText(i, str);
sz = pDC->GetTextExtent(str);
// Add the avg width to prevent clipping
sz.cx += tm.tmAveCharWidth;
if (sz.cx > dx)
dx = sz.cx;
}
// Select the old font back into the DC
pDC->SelectObject(pOldFont);
pComboBox->ReleaseDC(pDC);
// Adjust the width for the vertical scroll bar and the left and right border.
dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2 * ::GetSystemMetrics(SM_CXEDGE);
// If the width of the list box is too small, adjust it so that every
// item is completely visible.
if (pComboBox->GetDroppedWidth() < dx)
{
pComboBox->SetDroppedWidth(dx);
ASSERT(pComboBox->GetDroppedWidth() == dx);
}
}
Call it efter all the items have been added to the combobox:
//CComboBox m_HavingRole; // in header file
for (RoleList::iterator it = rolesObList.begin(); it != rolesObList.end(); it++)
{
nItem = m_HavingRole.AddString(it->m_szName);
m_HavingRole.SetItemData(nItem, it->m_lRoleID);
}
SetBestDroppedWidth(&m_HavingRole);

pshape processing multiple

I'm trying to make a PShape SVG multipy. I want a new shape created every time a variable (that I'm importing from a CSV file) changes. I tried using a for, but it doesn`t respect the variable range I'm giving it, it just creates as many SVGs as it wants. Basically what I'm trying to do is that if the variable indicates there are 21 data between an X rage, draw 21 copies of the SVG in a fixed distance between one and other.
Table table;
PShape tipi2;
PShape tipi3;
void setup() {
size (1875, 871);
table = loadTable("WHO.csv", "header");
tipi2 = loadShape("tipi-02.svg");
}
void draw() {
background(0);
for (TableRow row : table.rows()) {
int hale = row.getInt("Healthy life expectancy (HALE) at birth (years) both sexes");
}
tipi2.disableStyle();
noStroke();
for( int i = 0 ;i<=1800;i=i+33){
pushMatrix();
translate(0,89.5);
if(hale > 40 && hale < 60){
shape(tipi2,i,0);
popMatrix();
}
}
There are a couple of things that couple things that could be improved in your current code:
the hale variable's visibility (or scope) is only within this loop: for (TableRow row : table.rows()) {
the drawing styles (noStroke()/disableStyle(),etc.) don't change much therefore could e set once in setup() rather than multiple times a second in draw()
you could move the for loop from 0 to 1800 inside the for (TableRow row : table.rows()) { loop, but that might not be very efficient:
Here's what I mean:
Table table;
PShape tipi2;
PShape tipi3;
void setup() {
size (1875, 871);
table = loadTable("WHO.csv", "header");
tipi2 = loadShape("tipi-02.svg");
//this styles could be set once in setup, rather than multiple times in draw();
tipi2.disableStyle();
noStroke();
background(0);
for (TableRow row : table.rows()) {
int hale = row.getInt("Healthy life expectancy (HALE) at birth (years) both sexes");
for ( int i = 0; i<=1800; i=i+33) {
pushMatrix();
translate(0, 89.5);
//hale is visible within this scope, but not outside the for loop
if (hale > 40 && hale < 60) {
shape(tipi2, i, 0);
}
//popMatrix(); should be called the same amount of times as pushMatrix
popMatrix();
}
}
}
void draw() {
}

Setting the Orientation of the device based on Height and width

In my app I am having situation where all the phone will have only Portrait, Tablet with height = 1024 ; width = 768 then Height/width(1024/768<=1.3333) I want to make the tab Landscape otherwise I need to make Portrait.
String userAgent = new WebView(activity).getSettings()
.getUserAgentString();
double screen_size = 1.3333333333333333;
if (userAgent.contains("Mobile")) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mEditor.putString("Device_Mode", "Phone");
mEditor.commit();
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);
double height = size.y;
double width = size.x;
double aspect_Ratio = width / height;
if (aspect_Ratio <= screen_size) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
mEditor.putString("Device_Mode", "TAB-LANDSCAPE");
mEditor.commit();
quit();
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mEditor.putString("Device_Mode", "TAB-PORTRAIT");
mEditor.commit();
quit();
}
}
Screen is getting flicking and app is getting crash at last.
Check weather the mobile or tablet by using following code.
TelephonyManager manager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){
return "Tablet";
}else{
return "Mobile";
}
The screen got flickering because it is keep on switching between the orientation. So I've stopped by
android:configChanges="orientation|keyboardHidden|screenSize"
putting this line code in my manifest on the corresponding activity. So the activity flickering is stopped.
int i = getResources().getConfiguration().orientation;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);
double height = size.y;
double width = size.x;
double aspect_Ratio = 0.0;
if (i == 1)
aspect_Ratio = height / width;
else if (i == 2)
aspect_Ratio = width / height;
if (aspect_Ratio <= screen_size) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
By using this code I have achieved my task :)

Unity3d Box Collider Vertex

How do I get the vertexes of a box collider in world space in Unity3d? (with correct rotation and scale). Just doing local-world on the object does not work.
obj.transform.localToWorldMatrix.MultiplyPoint3x4(extents); and doing similar things for the other corners is what I tried.
Since you can easily get the BoxCollider's centre and extents you can get the local vertex position easily by adding or subtracting the extent from the centre, then transforming it into global world space with a method like Vector3.TransformPoint()
The following script prints the global position of each vertex of a BoxCollider attached to the same GameObject as the script, every Update() call.
using System.Collections;
public class DebugPrintColliderVertices : MonoBehaviour
{
const uint NUM_VERTICES = 8;
private BoxCollider boxCollider;
private Transform[] vertices;
void Awake()
{
boxCollider = (BoxCollider)this.gameObject.GetComponent(typeof(BoxCollider));
if (boxCollider == null)
{
Debug.Log ("Collider not found on " + this.gameObject.name + ". Ending script.");
this.enabled = false;
}
vertices = new Transform[NUM_VERTICES];
}
void Update()
{
Vector3 colliderCentre = boxCollider.center;
Vector3 colliderExtents = boxCollider.extents;
for (int i = 0; i != NUM_VERTICES ; ++i)
{
Vector3 extents = colliderExtents;
extents.Scale (new Vector3((i & 1) == 0 ? 1 : -1, (i & 2) == 0 ? 1 : -1, (i & 4) == 0 ? 1 : -1));
Vector3 vertexPosLocal = colliderCentre + extents;
Vector3 vertexPosGlobal = boxCollider.transform.TransformPoint(vertexPosLocal);
// display vector3 to six decimal places
Debug.Log ("Vertex " + i + " # " + vertexPosGlobal.ToString("F6"));
}
}
}
Note that the BoxCollider's size is multiplied by the transform.scale of the GameObject it's attached to. I haven't included that calculation in said script, but finding the overall rotation/scale should be fairly trivial from this point.

Resources